我是uitableviewcell的子类,以便我可以为我的所有单元格应用标准背景和文本,这是我的第一次尝试,但我主要展示了我的想法。虽然我被困在一个问题上。我的表有两组,我希望第一组以文本为中心,我希望第二组对齐左边。但此刻没有这样的运气。
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 = UITextAlignmentCenter;
}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;
}
有人有任何建议吗?
答案 0 :(得分:2)
您误解了可重复使用的细胞是如何工作的。当您致电dequeueReusableCellWithIdentifier
时,您将返回已分配的单元格。它可以用于代码中的任一组。
这是没有意义的代码:
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];
}
你正在取一个小区。如果它不存在,那么您设置居中(没有任何作用;请记住,cell
是nil
)。然后创建一个。
但你真正要做的是在中提取或创建单元格之后设置居中。你不在乎如何得到它;你只想重新配置当前值。
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;
}