【发布时间】:2013-07-17 20:26:27
【问题描述】:
概述
我使用情节提要在 UIScrollView 中添加了 UITableView。
我的简单目标是在这个表格视图中设置 UITableViewCell 的样式。
代码
目前,我已将 UITableView 属性添加到视图控制器并在情节提要中添加了插座。
@interface NWDetailViewController : UIViewController<UITableViewDelegate,
UITableViewDataSource> {
IBOutlet UIScrollView *scrollView;
...
IBOutlet UITableView *tableView;
IBOutlet NSLayoutConstraint *tableViewHeightConstraint;
}
创建了一个自定义的UITableViewCell(NWLocationTableViewCell),添加了一个属性并连接了storyboard中的outlet:
@interface NWLocationTableViewCell : UITableViewCell {
IBOutlet UILabel *titleLabel;
}
这就是我试图在我的控制器中填充单元格的方式:
-(UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"LocationCell";
// Same with both tableView and thisTableView
NWLocationTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[NWLocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
[cell.titleLabel setText:@"test"];
// or
UILabel *label = (UILabel *)[cell.contentView viewWithTag:2112];
[label setText:@"test"];
NSLog(@"%@, %@", cell.contentView.subviews, cell.class);
return cell;
}
但是这两种方法都没有产生任何结果,因此我无法设置它的样式。添加任何静态对象(例如 UIButton)也不会显示。
日志显示:"(), NWLocationTableViewCell"
【问题讨论】:
-
您有任何错误吗?你怎么知道你没有得到一个细胞?
-
我不完全确定这是否会有所帮助,但请尝试将您的插座从牙套中取出。将它们声明为大括号下方和
@end之前的属性,例如@property (strong, nonatomic) IBOutlet UILabel *titleLabel; -
@rdelmar 没有错误,nil 表示没有单元格
-
@gsean 尝试了两种方法,但谢谢
-
您的意思是日志中的 nil 吗?这意味着没有标题标签和标签,不一定没有单元格。你应该只记录单元格。如果我没记错的话,如果 cellFroRowAtIndexPath 不返回任何单元格,则会出现错误。
标签: ios objective-c uitableview uiscrollview