views:

219

answers:

2

Hello,

I am new to iPhone development and I am currently working on a simple RSS reader app. The problem I am having is that I need to reposition the textLabel inside the UITableViewCells. I have tried setFrame or setCenter but it doesn't do anything. Does anyone know what I need to do inside the tableView:cellForRowAtIndexPath: method to reposition the textLabel at the top of the cell (x = 0, y = 0)?

Thank you

PS: The UITableViewCell is referenced by a variable called cell. I have tried [cell setFrame:CGRectMake(0, 0, 320, 20)] with no success.

+1  A: 

Seems I solved my own problem. Here's some code, in case someone runs into the same problem:

UILabel *ttitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)] autorelease];
ttitle.font = [UIFont boldSystemFontOfSize:13];
ttitle.textColor    = [UIColor blackColor];
ttitle.textAlignment = UITextAlignmentLeft;

[ttitle setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];

[cell.contentView addSubview:ttitle];

The idea is to create your own label object, because the textLabel is automatically positioned and can't be moved around.

Cheers.

Marian Busoi
+2  A: 

You can create a subclass for UITableViewCell and customize de textLabel frame. See that answer: Labels aligning in UITableViewCell. It's works perfectly to me.

It's my subclass

#import "UITableViewCellFixed.h"
@implementation UITableViewCellFixed
- (void) layoutSubviews {
     [super layoutSubviews];
     self.textLabel.frame = CGRectMake(0, 0, 320, 20);
}
@end

It's my UITableViewControllerClass:

UITableViewCellFixed *cell = (UITableViewCellFixed *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCellFixed alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
wal