在UITableView中使用UITableViewCell
我有点困惑。 我想创build一个自定义单元格,我想使用界面生成器的方式。
我创build一个表的正常方式是将表格设置为:
。H
@interface AssessList : UIViewController { IBOutlet UITableView *tblAssessList; } @property(nonatomic, retain) UITableView *tblAssessList; @end
.M
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return groupArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return totalArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = @"I am the text...."; return cell; }
现在我已经为单元格创build了一个新的类,我想我理解如何将其放入。但是,我可以将.h作为
@interface AssessList : UIViewController
或者使用全表的类/笔尖必须是UITableViewController?
汤姆
那么带有完整表格的类/笔尖必须是UITableViewController?
不, UITableViewController
只是一个方便的UIViewController
子类,它有一个UITableView
,并且已经被设置为它的委托/数据源(它被声明为符合UITableViewDelegate
和UITableViewDatasource
协议),它也有这些协议的预填充方法实现Xcode为您生成的模板实现文件。 你可以自己做所有这些,我经常这样做。
但是,您应该为您的UITableViewCell制作一个IBOutlet,以便您可以从nib文件中加载它(请参阅“表视图编程指南”中的“ 加载自定义表格 – 从Nib文件中查看单元格 ”)。
如果你想在Interface Builder Way中做,那么创build一个xib(查看xib)。 从obj面板中拖放一个UITableViewCell对象。 自定义它,如你所愿。 在tableView:cellForRowAtIndexPath:方法中,执行以下操作:
UITableViewCell * aCell = [tableview dequeueReusableCellWithIdentifier:@"SomeIdentifier"]; if (aCell == nil) { NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"CustomCellNibName" owner:self options:nil]; for (NSObject *anObj in arr) { if([anObj isKindOfClass:[UITableViewCell class]]) { aCell = (UITableViewCell *)anObj; } } }
tableviewcell的标识符可以在IB中设置。
你可以用这个示例代码描述听到。
http://ijoshsmith.com/2011/07/16/creating-a-custom-uitableviewcell-in-ios-4/
或者你也可以使用这个链接
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
我想这应该是UItableViewCell的子类
即
@interface AssessList : UITableViewCell
当你想要一个自定义的tableview单元格时,你也需要一个UITableViewCell的子类。
一个教程可以在这个博客上find
请记住,可以在不创build自定义单元格的情况下完成很多操作,包括添加开关以使tableview看起来像来自settings.app的那个,以及iPod显示歌曲的方式。
在您正在使用在otherviewController(UITableViewCell子类)中创build的自定义单元的评估列表类,所以没有必要改变这一行
@interface AssessList:UIViewController
注意: – otherviewController应该是UITableViewCell的一个子类