开发者

How can I display two rows in the same UITableViewCell?

I'm trying to show two rows of text in the same Table Row of a UITableView.

Is this possible to do?

I've tried different things, but I'm not able to find anything about it or figure it out. Any help you can provide would be appreciated.

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

    UITableViewCell *cell = [tableView dequeueReusableCellW开发者_StackOverflow中文版ithIdentifier: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;
}


You can see the different style in Apple's Table View Programming Guide for iOS, what you may want is the UITableViewCellStyleSubtitle (Figure 1-7 in the guide). Please have a look.

You can set the second label with cell.detailTextLabel.text = @"my text".

You've got to change this line

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

to

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

using the right style: UITableViewCellStyleSubtitle.

EDIT

To format a string like you described in your comment:

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

EDIT 2

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

Please have a look at String Programming Guide / Formatting String Objects for more detail on how to format numbers etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜