自定義uitableviewcell xib

  1. //網上教程很多,但是都沒有將到點上,自己總結一下:  
  2.   
  3. 1、新建類,基於UITableViewCell  
  4. 例如  @interface CellSearch : UITableViewCell  
  5.   
  6. 2、新建空 xib 文件  
  7. 例如命名爲   CellSearch.xib  (名字要跟類名一樣)  
  8. 拖一個UITableViewCell控件到xib中  
  9. (你可以在這個cell裏面放你想放的東東)  
  10.   
  11. 3、使用  
  12. 經過12步,自定義的cell已經建好了  
  13. 在cellForRowAtIndexPath  方法中使用  
  14.   
  15. CellSearch *cell = [tableView dequeueReusableCellWithIdentifier:@"CellSearch"];  
  16.     if(!cell) {  
  17.         cell = [[[NSBundle mainBundle] loadNibNamed:@"CellSearch" owner:self options:nil]lastObject];  
  18.     }   
  19. return cell;  
  20.   
  21.   
  22. 運行看效果  
  23.   
  24. 4、關聯xib與類文件(這個關聯我經常弄錯)  
  25. (到第3步,你已經可以看到自定義的cell了,但是還不能獲取cell裏面的信息,因爲沒有關聯)  
  26. 選中CellSearch.xib中的UITableViewCell控件,將類名關聯爲CellSearch  
  27. (然後就是Cell內普通的控件關聯了)  
  28.   
  29. 5、使用  
  30. 在cellForRowAtIndexPath  方法中使用  
  31. 例如:  
  32. CellSearch *cell = [tableView dequeueReusableCellWithIdentifier:@"CellSearch"];  
  33.     if(!cell) {  
  34.         cell = [[[NSBundle mainBundle] loadNibNamed:@"CellSearch" owner:self options:nil]lastObject];  
  35.     }   
  36. cell.nameTextField.text = @"tom";  //關聯後可以直接設置cell內的控件  
  37. return cell;  
  38.   
  39. 比較懶,就不附圖了  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章