使用子视图重用Cell(以编程方式)

时间:2011-06-14 01:21:22

标签: iphone ios uitableview uitextfield

我遇到了以编程方式将子视图添加到UITableView单元格的问题。我试图将UITextFiled添加到2个单元格,并将UILabel添加到我表格的一个部分中的另一个单元格。该表有3个部分,其中大部分是手动创建的。其中一个单元格在选中时会调用一个插入另外四个单元格的操作。如果再次选择相同的单元格,则将移除四个单元格。发生此操作时,子视图会混淆并位于错误的单元格上。键盘也不响应完成键。

我尝试在if(cell == nil)条件语句中创建子视图。这是我的代码创建单元格(抱歉它太乱了,我一直在尝试很多东西):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSLog(@"cell created");
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if (indexPath.section == 0 && indexPath.row == 0 && ([cellText objectAtIndex:indexPath.row] == @"Weight")) {
        exerciseWeight = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, 295, 30)] autorelease];
        exerciseWeight.textAlignment = UITextAlignmentRight;
        exerciseWeight.adjustsFontSizeToFitWidth = YES;
        exerciseWeight.textColor = [UIColor blackColor];
        exerciseWeight.backgroundColor = [UIColor clearColor];
        exerciseWeight.tag = 89899;
        exerciseWeight.keyboardType = UIKeyboardTypeNumberPad;
        [exerciseWeight setReturnKeyType:UIReturnKeyDone];
        exerciseWeight.clearButtonMode = UITextFieldViewModeNever;
        [exerciseWeight setEnabled: YES];
        [exerciseWeight setDelegate:self];
        [cell addSubview:exerciseWeight];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;

    } else if (indexPath.section == 0 && indexPath.row == 1) {
        NSLog(@"Created metric subview");
        metric = [[[UILabel alloc] initWithFrame:CGRectMake(100, 5, 180, 30)] autorelease];
        metric.textAlignment = UITextAlignmentRight;
        metric.textColor = [UIColor grayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        metric.tag = 89898;
        [cell addSubview:metric];
    } else if (([cellText count] - 1) == indexPath.row){
        exerciseReps = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, 295, 30)] autorelease];
        exerciseReps.textAlignment = UITextAlignmentRight;
        exerciseReps.adjustsFontSizeToFitWidth = YES;
        exerciseReps.backgroundColor = [UIColor clearColor];
        exerciseReps.tag = 89890;
        exerciseReps.keyboardType = UIKeyboardTypeNumberPad;
        [exerciseReps setReturnKeyType:UIReturnKeyDone];
        exerciseReps.clearButtonMode = UITextFieldViewModeNever;
        [exerciseReps setEnabled:YES];
        [exerciseReps setDelegate:self];
        [cell addSubview:exerciseReps];
    } else if (indexPath.section == 2) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
} else {
    exerciseWeight = (UITextField *)[cell viewWithTag:89899];
    metric = (UILabel *)[cell viewWithTag:89898];
    exerciseReps = (UITextField *)[cell viewWithTag:89890];
}
if (indexPath.section == 0) {
    cell.textLabel.text = [cellText objectAtIndex:indexPath.row];
}
// Configure the cell...
if (indexPath.section == 0 && indexPath.row == 0) {
    cell.textLabel.textAlignment = UITextAlignmentLeft;
    exerciseWeight.placeholder = @"160";


} else if (indexPath.section == 0 && indexPath.row == 1) {
    metric.text = metricUnit;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (cell.textLabel.text == @"Repetitions"){
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    exerciseReps.placeholder = @"10";
} else if (indexPath.section == 1) {
    cell.textLabel.text = @"Log New Set";
    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.accessoryType = UITableViewCellAccessoryNone;
} else if (indexPath.section == 2) {
    cell.textLabel.text = @"History";

} else if (cell.textLabel.text == @"  Pounds") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Kilograms") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Miles") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Kilometers") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
}

return cell;

// [exerciseWeight release];
// [metric release];
// [exerciseReps release]; 
}

1 个答案:

答案 0 :(得分:1)

看起来你会在这里玩得开心。但是一旦你完成它将会令人满意: - )

我不确定你发布的代码是否需要担心。定义你在表格中看到的内容(主要是)是heightForRowAtIndexPath,numberOfSectionsInTableView和numberOfRowsInSection等方法。如果你做对了,你就会得到你想要的显示器。因此,当您点击一个单元格时,您将修改底层数据结构,然后调用reloadData - 使表视图再次调用这些方法。

关于布置这些单元格,我没有看到你使用单元格的contentView。你有没有看过Cocoa With Love博客,特别是Easy custom UITableView drawingUITableView construction, drawing and management (revisited)?我还使用了定义表视图单元格的xib方法,如Apple文档Table View Programming Guide for iOS中所述,并且考虑到Xcode不断改进界面构建器与代码的交互的方式可能会继续这样做。我非常反对IB,但现在看到使用它的价值,因为Xcode 4消除了切换应用程序使用它的痛苦,犯错误的机会要少得多。

我在想的是,当您向单元格“添加视图”时,您应该切换到已定义的另一个单元格,并根据需要在reloadData之后加载单元格。当您想要添加单元格时,您可以更改基础数据结构并让表格显示出来。

关于单元格标识符的问题 - 您可以使用一个单元格标识符,只保存创建单元格的成本,分解您对所有单元格执行的常见操作,或者您可以创建每种类型具有唯一标识符的单元格当您的部分/行表示需要TK421时,细胞和再利用细胞变体TK421。我倾向于做第二个。我实际上并不知道哪一个表现更好,但我确信要么比没有重用要好。