【发布时间】:2014-01-22 21:33:11
【问题描述】:
我正在尝试获取一个表格来显示名称和等级列表。在每个单元格中,我希望有 2 个 UILabel 作为子视图——一个 UILabel 显示排名,一个 UILabel 显示名称。
我创建了一个 UITableViewCell 子类,它有如下两个属性:
@interface MyTableViewCell : UITableViewCell
@property UILabel* rankLabel;
@property UILabel* nameLabel;
除了每个属性的@synthesize 之外,我没有在 MyTableView 的 .m 实现文件中添加任何内容。实现文件的其余部分正是 xCode 在您创建 UITableViewCell 子类时为您提供的内容。
然后,在包含该表的 masterViewController 中,我有以下内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Person *object = _objects[indexPath.row];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.height, 0, cell.frame.size.width - cell.frame.size.height, cell.frame.size.height)];
cell.nameLabel.text = object.name;
[cell addSubview:cell.nameLabel];
NSString* rankString = [object.rank stringValue];
cell.rankLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.height, cell.frame.size.height)];
cell.rankLabel.text = rankString;
[cell addSubview:cell.rankLabel];
}
当我在调试中运行它时,它会到达我设置 cell.nameLabel 并分配它的第一行,并引发以下错误:
[UITableViewCell setNameLabel:]: unrecognized selector sent to instance 0x108fa64c0
真的不明白为什么 setNameLabel 方法在这里应该有问题。有任何想法吗?提前致谢。
【问题讨论】:
-
您根本不使用情节提要吗?或者只是不适用于这个特定的单元格?
标签: ios objective-c uitableview