1

I do have this working, but I'm wondering why the method I thought should work, does not work. I have a .xib with a uitableviewcell and WirelessCell.h/.m as the class. I wanted to add an object called ADVPercentProgressBar to the cells. My cellForRowAtIndexPath looks like this:

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

}

This works fine adding the ADVPercentProgressBar during the cell initialization, but I thought I could put the ADVPercentProgressBar in the cells initWithStyle like this:

- (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];

    }

and not in the ViewController cell initialization. but this doesn't work. I'm just wondering why, as it seems logical to me to add objects to the cell class initialization. return self; }

4

1 に答える 1

0

Xibからセルをロードしている場合、initメソッドはuseとは呼ばれませんawakeFromNib。また、そのようにして、すべてのオブジェクトがインスタンス化され、すべてのアウトレットが設定されます。

于 2012-04-08T14:27:10.167 に答える