Jesper Martensson
Jesper Martensson

Reputation: 1248

Adding a third label to a cell?

As title says I would like to have 3 labels in my cell (in tableView). As can be seen in the code below I currently only have 2 labels which are name, as textLabel and book as detailTextLabel. But what if I also would like chapter as a label (own row in the tabelView cell)? What would be the best way to implement this?

The output should look like this in the tableView:

Name

Book

Chapter

/Thanks in regards!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BookmarkCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Bookmark *item = [self.items objectAtIndex:indexPath.row];

NSArray *chunks = [item.name componentsSeparatedByString: @","];

NSString *name;
NSString *book;
NSString *chapter;

if ([chunks count] > 0)
{
    name = [chunks objectAtIndex:0];
    if ([chunks count] > 1)
    {
        book = [chunks objectAtIndex:1];
        if ([chunks count] > 2)
        {
            chapter = [chunks objectAtIndex:2];
        }
    }
}

cell.textLabel.text = name;
cell.detailTextLabel.text = book;

Upvotes: 0

Views: 170

Answers (3)

DROP TABLE users
DROP TABLE users

Reputation: 1955

Sounds like your going to want a custom UITableViewCell and with that you can add anything you would like into it. From there just name the labels that you put into it and write the code accordingly to fill them with the cellForRowAtIndexPath method.

Upvotes: 1

Raymond Wang
Raymond Wang

Reputation: 1484

If what you need is simply another label, then you can do what mark says, create a subview and set it to your cell.

If you want to have something can do more, what you need is a custom UITableViewCell. You can define buttons and other controls. Take a look at this document apple provide. This document can really help to understand how UITableViewCell works so it's worth reading.

Upvotes: 0

Mark
Mark

Reputation: 2726

Create a custom UIView and add it as a subview to the cell's contentView. In this custom view add as many UILalels as you would like. You don't actually have to create a custom view to do this, but it allows for greater versatility.

Here is some basic code to achieve three labels.

UIView * pNewContentView= [[UIView alloc] initWithFrame:cell.contentView.bounds];
CGRect labelFrame= pNewContentView.bounds;
labelFrame.size.width= labelFrame.size.width * 0.33;

UILabel* pLabel1=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel1];

labelFrame.origin.x= labelFrame.size.width;
UILabel* pLabel2=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel2];

labelFrame.origin.x= labelFrame.origin.x + labelFrame.size.width;
UILabel* pLabel3=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel3];

[cell.contentView addSubview:pNewContentView];

Upvotes: 0

Related Questions