iphone - 识别子类 UITableViewCell 的 indexPath,这样我就可以指定我的 UILabel 的对齐方式

标签 iphone objective-c ios xcode

我正在对 uitableviewcell 进行子类化,以便我可以为我的所有单元格应用标准背景和文本,这是我的第一次尝试,但我主要是展示我想要的方式。虽然我被困在一个问题上。我的 table 有两组,我希望第一组的文本居中,我希望第二组向左对齐。但目前没有这样的运气。

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;
}

有人有什么建议吗?

最佳答案

您误解了可重用单元格的工作原理。当您调用 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];
}

您正在获取一个单元格。如果它不存在,则设置居中(什么都不做;记住,cellnil)。然后你创建一个。

但您真正想做的是在获取或创建单元格后设置居中。您不在乎如何获得它;您只想为当前值重新配置它。

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;
}

关于iphone - 识别子类 UITableViewCell 的 indexPath,这样我就可以指定我的 UILabel 的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6945240/

相关文章:

ios - 如何应用适用于 UITableView 和 UICollectionView 的延迟加载

ios - Xcode 8.2.1/Swift 3 - 从 Plist 字典数组加载 TableView

ios - UIView.transition动画它只工作一次,第二次图片消失

ios - 前台弹出通知

iphone - 如何从左翻转?

objective-c - 随机数选择器总是只在应用程序首次启动时选择相同的数字

objective-c - TSAlertView 启用设备旋转

ios - 如何使用 ‘done’ 按钮 Swift 4 隐藏键盘

iphone - iPhone 上的 "Untar"文件

ios - undefined 不是对象(评估 'navigation.mediaDevices.getUserMedia' )