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