如何解决重复使用带子视图的单元格以编程方式
| 我在以编程方式将子视图添加到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,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-导致表视图再次重新调用这些方法。
关于布局这些单元格,我根本看不到您使用该单元格的contentView。您是否看过Cocoa With Love博客,特别是Easy自定义UITableView绘图和UITableView的构造,绘图和管理(已重新访问)?我还使用了xib方法(如Apple文档,iOS的“表视图编程指南”中所述)来定义表视图单元格,并且鉴于Xcode一直保持改进接口构建器与代码交互的方式,这种方法可能会继续这样做。我非常反对IB,但是现在看到使用它的价值,因为Xcode 4消除了切换应用程序以使用它的痛苦,出现错误的机会更少了。
我在想的是,当您向某个单元格“添加视图”时,您应该只切换到您定义的另一个单元格,并根据需要在reloadData之后加载该单元格。当您要添加单元格时,您可以更改基础数据结构,并让表格自行显示。
关于单元格标识符的问题-您可以使用一个单元格标识符并仅节省创建单元格的成本,将您对所有单元格所做的所有常见事情都排除在外,或者您可以为每种单元格类型创建具有唯一标识符的单元格,当您的节/行指示需要TK421时,请重用单元变体TK421。我倾向于做第二个。我实际上不知道哪一个性能更好,但是我敢肯定,这两者总比没有重用要好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。