将对象添加到自定义可重用单元格

时间:2012-04-08 06:53:03

标签: objective-c xcode cocoa-touch uitableview

我确实有这个工作,但我想知道为什么我认为应该工作的方法,不起作用。 我有一个带有uitableviewcell的.xib和一个WirelessCell.h / .m作为类。 我想在单元格中添加一个名为ADVPercentProgressBar的对象。 我的cellForRowAtIndexPath如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int section = indexPath.section;
    int row = indexPath.row;

    if (section == 6) {
        // Make dynamic rows cell
        static NSString *CellIdentifier = @"wireless3";
        WirelessCell *cell = (WirelessCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (!cell) {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WirelessCell" owner:nil options:nil];
            cell = [topLevelObjects objectAtIndex:0];
            cell.progressBar = [[ADVPercentProgressBar alloc] initWithFrame:CGRectMake(10, 22, 300, 28) andProgressBarColor:ADVProgressBarBlue];
            [cell addSubview:cell.progressBar];
        }

        // Set labels here
        NSDictionary *tmpDict = [[[DataSingleton sharedSingleton] sharedWirelessClients] objectAtIndex:row];
        cell.labelUptime.text = [tmpDict objectForKey:@"uptime"];
        [cell.progressBar setProgress:[[tmpDict objectForKey:@"signal"] intValue]*0.001];

        return cell;

    } else {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }

}

在单元初始化期间可以正常添加ADVPercentProgressBar,但我想我可以将ADVPercentProgressBar放在单元格initWithStyle中,如下所示:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        progressBar = [[ADVPercentProgressBar alloc] initWithFrame:CGRectMake(10, 22, 300, 28) andProgressBarColor:ADVProgressBarBlue];
        [self.contentView addSubview:progressBar];

    }

而不是在ViewController单元初始化中。但这不起作用。 我只是想知道为什么,因为在对单元类初始化中添加对象似乎是合乎逻辑的。         回归自我;     }

1 个答案:

答案 0 :(得分:0)

如果从Xib加载单元格,则init方法不会被称为use awakeFromNib。同样,所有对象都被实例化,并且任何出口都被设置。

相关问题