是否可以在 TableView 中添加 2 个 cell.textlabel
。因为我想尝试显示 3 条不同的线。
这是我做的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size
cell.textLabel.numberOfLines = 2;
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@","Walk to and board at ",[boardDescArray objectAtIndex:indexPath.row]];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@","Alight Destination = ",[alDescArray objectAtIndex:indexPath.row]];
return cell;
}
但是当我放置两个 cell.textLabel.text
时出现错误 bad access
。我应该怎么办?
请帮忙
最佳答案
您只调用 cell.textLabel.text 一次,但在字符串中您想要换行的地方放置一个\n。
您的 stringWithFormat 中也有错误——要么在“Walk to and board at”前面放一个@,要么只删除第一个 %@,然后:
stringWithFormat:@"Walk to and board at %@",[boardDescArray objectAtIndex:indexPath.row]];
因此,要使其成为 2 行,您需要这样:
cell.textLabel.text = [NSString stringWithFormat:@"Walk to and board at %@\nAlight Destination = %@",[boardDescArray objectAtIndex:indexPath.row],[alDescArray objectAtIndex:indexPath.row]];
关于iphone - 如何将单元格添加到tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927198/