Identify indexPath for subclassed UITableViewCell so i can specify the alignment of my UILabel
I am subclassing uitableviewcell so that i can apply a standard background and text for all my cells, this is my first attempt at this but I have it mostly displaying how i would like. Although i am stuck on one issue. My table has two groups and i would like the first group to have the text centered and i would like to have the second group align to the left. But no such luck at this point.
CustomCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self];
NSLog(@"%i", indexPath);
int rows = [(UITableView *)self.superview numberOfRowsInSection:indexPath.section];
NSLog(@"%i", rows);
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
if (centerText) {
cellText = [[[UILabel alloc] initWithFrame:CGRectMake(0, 15, self.bounds.size.width - 10, 30)] autorelease];
cellText.textAlignment = UITextAlignm开发者_开发百科entCenter;
}else {
cellText = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, self.bounds.size.width - 10, 30)] autorelease];
cellText.textAlignment = UITextAlignmentLeft;
}
cellText.font = [UIFont boldSystemFontOfSize:16];
cellText.backgroundColor = [UIColor clearColor];
cellText.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5];
cellText.shadowOffset = CGSizeMake(0,1);
cellText.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.frame];
UIImage* img = [UIImage imageNamed:@"odd_slice.png"];
imgView.image = img;
self.backgroundView = imgView;
[imgView release];
UIImage *accessoryImage = [UIImage imageNamed:@"content_arrow.png"];
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage];
// accessoryView.image = accessoryImage;
self.accessoryView = accessoryView;
[accessoryView release];
//Selected State
UIImage *selectionBackground = [UIImage imageNamed:@"row_selected.png"];
UIImageView *selectionView = [[UIImageView alloc] initWithFrame:self.frame];
selectionView.image = selectionBackground;
self.selectedBackgroundView = selectionView;
[selectionView release];
//Adds Text
[self addSubview:cellText];
}
return self;
}
TableView.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSArray *keys = [[appDelegate rowersDataStore] allKeys];
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
switch (indexPath.section) {
case 0:
[cell setCenterText:YES];
break;
case 1:
[cell setCenterText:NO];
break;
default:
break;
}
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch (indexPath.section) {
case 0:
[cell.cellText setText:@"Create New Rower"];
break;
case 1:
[cell.cellText setText:[keys objectAtIndex:indexPath.row]];
break;
default:
break;
}
// Set up the cell...
return cell;
}
Anyone have any suggestions?
You're misunderstanding how reusable cells work. When you call dequeueReusableCellWithIdentifier
you're getting back a cell that was already allocated. It could have been used for either group in your code.
This is the code that makes no sense:
if (cell == nil) {
switch (indexPath.section) {
case 0:
[cell setCenterText:YES];
break;
case 1:
[cell setCenterText:NO];
break;
default:
break;
}
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
You're fetching a cell. If it doesn't exist, then you set the centering (which does nothing; remember, cell
is nil
). You then create one.
But what you really mean to do is to set the centering after you've fetched or created the cell. You don't care how you get it; you just want to reconfigure it for the current values.
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch (indexPath.section) {
case 0:
[cell setCenterText:YES];
[cell.cellText setText:@"Create New Rower"];
break;
case 1:
[cell setCenterText:NO];
[cell.cellText setText:[keys objectAtIndex:indexPath.row]];
break;
default:
break;
}
精彩评论