iphone - 重用带有 subview 的单元格(以编程方式)

标签 iphone ios uitableview uitextfield

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

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

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

最佳答案

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

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

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

我的想法是,当您向单元格“添加 View ”时,您应该只切换到您定义的另一个单元格,并在 reloadData 之后根据需要加载该单元格。当您想要添加单元格时,您需要更改底层数据结构并让表格自行显示。

关于单元格标识符的问题——您可以使用一个单元格标识符并仅节省创建单元格的成本,分解出您对所有单元格所做的所有常见操作,或者您可以为每种类型创建具有唯一标识符的单元格当您的部分/行指示需要 TK421 时,使用电池并重复使用电池变体 TK421。我倾向于做第二种。我实际上并不知道哪一个表现更好,但我确信其中任何一个都比完全不重用要好。

关于iphone - 重用带有 subview 的单元格(以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6338130/

相关文章:

iphone - 如何根据另一个选择器的选择更改选择器行?

iphone - 通过 JSON 传递西类牙语数据

ios - 使用 CoreMotion 在后台获取加速度计数据

ios - 如何为下次启动存储 UITableView 内容?

javascript - 将javascript放入uiwebview问题

ios - 与 jenkins 一起使用的 xcodebuild 测试命令不起作用

ios - 在 Swift 中计算数据时如何改进 UITableView 滚动?

ios - 如何根据来自服务器的标志更改 uitableviewcell 的颜色

ios - 重用单元格时,如何将 UISegmentedControl 的选定段保留在 UITableView 中?