Bar*_*rtu 3 iphone uitableview ios
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell2";
UILabel *titleLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 300, 20)];
[cell.contentView addSubview:titleLabel];
titleLabel.tag = 0011;
}
else
{
titleLabel = (UILabel *)[cell.contentView viewWithTag:0011];
}
// Configure the cell...
NSMutableString *title = [NSMutableString stringWithString:@"Customer: "];
[title appendString:[titles objectAtIndex:indexPath.row]];
titleLabel.text = title.copy;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentLeft;
titleLabel.font = [UIFont systemFontOfSize:18.0];
Run Code Online (Sandbox Code Playgroud)
我的单元格永远不会是零,而我的标题标签,因为它从未被分配,尽管我的单元格已生成.我看不出这是怎么可能的.if状态永远不会是真的,应该是第一次生成的单元格,但是我的单元格应该是按原样创建的,而不是我的titleLabel的
听起来好像您使用的是iOS 5(或更高版本)和Storyboard.
在iOS 5(或更高版本)下,如果您使用的是Storyboards和TableView控制器,dequeueReusableCellWithIdentifier:则保证该方法返回一个单元格(前提是您已在Storyboard中定义了具有给定标识符的单元格).
如果是这种情况,解决方案是在Storyboard中完全创建自定义表格单元格.转到故事板中的表格视图,选择Content:Dynamic Prototypes并制作Prototype Cells:1.现在以图形方式布置单元格,使其完全符合您的要求.现在单击单元格并进行设置Identifier:Cell2.您现在不需要在运行时创建标签或检查它是否为零.有关如何引用已设置的标签的完整详细信息,请参阅iOS 5发行说明(下面的链接)或网络上的许多教程.
请参阅iOS 5发行说明部分配置表视图
| 归档时间: |
|
| 查看次数: |
2149 次 |
| 最近记录: |