How can I change the position of the text in a UITableView?

How can I change the location of the text AND show the FULL row in the UITableView?

Here is a screenshot of what I have, as soon as you look at it, you know what I mean. thanks to My app http://img188.imageshack.us/img188/9926/chucknhelp.tif

+1


a source to share


3 answers


You can do this without subclassing UITableViewCell

.

In the method, tableView:cellForRowAtIndexPath:

create a base UITableViewCell

and change the properties UILabel

contained in this cell. Change the maximum number of lines to add more text to the cell.

Unfortunately the cell label is not available by properties, so you need to get it using a property subviews

.

Of course this uses an implementation detail UITableViewCell

, so it might break with future SDK versions. Use with care.

Here's an example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"YourCellId"];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"YourCellId"] autorelease];
        cell.font = [UIFont systemFontOfSize:16];
        UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0];
        [cellLabel setNumberOfLines:3];
    }
    cell.text = @"Your text";
    return cell;
}

      

Update:



To set the line-height to match the text, you can do the following:

1. Set the height of the cell to match the text.

You need something like this:

CGSize textSize = [text sizeWithFont:font
                   constrainedToSize:CGSizeMake(313, 1000)
                       lineBreakMode:UILineBreakModeWordWrap];
cell.frame = CGRectMake(0.0f, 0.0f, 320.0, textSize.height);

      

2. Return the height of the cell to the method tableView:heightForRowAtIndexPath:

.

For example, for example:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [self tableView:(UITableView*)self.view cellForRowAtIndexPath:indexPath];
    CGRect cellFrame = cell.frame;
    return cellFrame.size.height;
}

      

+2


a source


Subclass UITableViewCell

, and in the subclass loadView

create a UILabel inside contentView

. Set the appropriate packaging and location for this label.



0


a source


Adam's got it right. For more details (as well as a method that scrolls quickly) you can check this post on atebits.com which bypasses the UILabel approach entirely.

Lauren Brichter Example UITableViewCell Subclasses

You can also see the TableViewSuite example from developer.apple.com It contains 5 different examples, in increasing complexity, for creating custom table views. The last example is structurally very similar to the tutorial on atebits.com.

Lecture 8 of the Stanford iPhone course also deals with scrolling and tabular views, and some examples of subclassing UITableViewCell.

iPhone course

0


a source







All Articles