我有两个数组
1)名称数组;2)数字数组;
这些数组包含名称和数字,我想在不使用 nib 的情况下在 tableview 中显示这些名称和数字。如何以编程方式创建自定义单元格
我有两个数组
1)名称数组;2)数字数组;
这些数组包含名称和数字,我想在不使用 nib 的情况下在 tableview 中显示这些名称和数字。如何以编程方式创建自定义单元格
创建一个应该是 UITableViewCell 子类的新文件。在这里你可以写你可以创建 2 个标签,你可以在任何你想要的地方显示。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *theID = @"TableViewCell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:theID];
return cell;
}
这里的 CustomCell 是 UITableViewCell 的子类
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"CellIdentifier";
CustomCellName *cell = (CustomCellName *)[tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell) {
cell = [[CustomCellName alloc] initWithStyle:UITableViewStyleDefault reusableIdentifier:identifier];
}
cell.textLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.subtextLabel.text = [numberArray objectAtIndex:indexPath.row];
return cell;
}
上面的代码没有经过测试,我不是在计算机上执行此操作(而是在我的 iPhone 上全部输入),但它应该是您需要的代码。
textLabel = 您为自定义单元格类中的标签设置的属性名称 subtextLabel = 您为自定义单元格类中的标签设置的属性名
希望这可以帮助!