objective-c - 如何在同一个 UITableViewCell 中显示两行?

标签 objective-c ios uitableview

我试图在 UITableView 的同一表格行中显示两行文本。

这可能吗?

我尝试过不同的方法,但我无法找到任何相关信息或弄明白。如果您能提供任何帮助,我们将不胜感激。

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    phonesAppDelegate *appDelegate = (phonesAppDelegate *)[[UIApplication sharedApplication] delegate];
    HLPFoundPhones *p = (HLPFoundPhones *)[appDelegate._phones objectAtIndex:indexPath.row];

    NSString *subtitle = [NSString stringWithFormat:@"Found on location (",p.x,@",",p.y,@") on date ", p.date];

    cell.textLabel.text = p._name;
    cell.detailTextLabel.text = subtitle;

    return cell;
}

最佳答案

你可以在Apple的Table View Programming Guide for iOS中看到不同的风格,您可能需要的是 UITableViewCellStyleSubtitle(指南中的图 1-7)。请看一下。

您可以使用 cell.detailTextLabel.text = @"my text" 设置第二个标签。

你必须改变这一行

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

使用正确的样式:UITableViewCellStyleSubtitle。

编辑

要像您在评论中描述的那样格式化字符串:

NSString *location = @"a Location";
NSString *date = @"20m ago";
NSString *subtitle = [NSString stringWithFormat:@"%@ on %@", location, date];

编辑 2

NSString *subtitle = [NSString stringWithFormat:@"Found on location ("%f","%f") on date %@", p.x, p.y, p.date];

请看String Programming Guide / Formatting String Objects有关如何格式化数字等的更多详细信息。

关于objective-c - 如何在同一个 UITableViewCell 中显示两行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5920119/

相关文章:

objective-c - 这真是聪明吗 [__persistentStoreCoordinator 版本], __persistentStoreCoordinator = nil;

ios - 使用自动布局时增加 UITextField 高度

ios - 为什么 NSMapTable 抛出异常 "count underflow"?

ios - 如何将对象数组从 iPhone 传递到 Apple Watch。只应传递某些字段

ios - 无法更改 UITableViewCell subview 的位置

ios - 带有文本字段的 UiTableView - 出现键盘时滚动

ios - 如何从方法中启动 ViewController?

ios - 如何在 UITableViews 中编辑多个部分的标题?

ios - UITableViewCell 中的 UIActivityIndi​​catorView(微调器)无法通过 UITableViewController 快速启动动画

ios - 当用作函数中的参数时,结构值类型前面需要关键字 var