我正在尝试在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以获取有关如何格式化数字等的更多详细信息。

08-05 23:03
查看更多