[英]Reusing UITableView Cells causes crash when a cell is “Recycled”
我正在尝试使用标签和单元格标识符重用cellViews,但是,只要重新使用单元格,下面的代码就会崩溃。 我想我快到了。 谁能看到错误?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
const NSInteger BUTTON_TAG = 1001;
const NSInteger SWITCH_TAG = 1002;
const NSInteger TEXTFIELD_TAG = 1003;
NSString *CellIdentifier = @"";
if(indexPath.section == 2 && indexPath.row == 0)
CellIdentifier = @"Button";
else if (indexPath.section == 3)
CellIdentifier = @"Switch";
else
CellIdentifier = @"TextField";
UISwitch *switchView;
UITextField *textField;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (CellIdentifier == @"TextField")
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGRect frame = CGRectInset([cell.contentView bounds], 70, 10);
textField = [[[UITextField alloc] initWithFrame:frame] autorelease];
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
cell.accessoryView = textField;
cell.tag = TEXTFIELD_TAG;
}
else if (CellIdentifier == @"Button")
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.clipsToBounds=YES;
cell.tag = BUTTON_TAG;
}
else if (CellIdentifier == @"Switch")
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
switchView = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = switchView;
cell.tag = SWITCH_TAG;
}
}
else
{
textField = (UITextField*)[cell viewWithTag:TEXTFIELD_TAG];
switchView = (UISwitch*)[cell viewWithTag:SWITCH_TAG];
}
崩溃日志
2012-02-22 14:50:08.352 ***[2304:207] -[UITableViewCell setSecureTextEntry:]: unrecognized selector sent to instance 0x6368270
2012-02-22 14:50:08.355 ***[2304:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setSecureTextEntry:]: unrecognized selector sent to instance 0x6368270'
在注释的单元格分配行旁边,您正在将.tag属性设置为单元格,而不是文本字段和开关,这很可能是导致崩溃的原因。 还要发布一个崩溃日志,这样我们就可以确切地了解导致应用崩溃的原因。
您没有说崩溃是什么,也没有提供回溯,但是我立即看到的一个问题是您一直在做:
switchView = (UISwitch*)[cell viewWithTag:SWITCH_TAG];
即使三种类型中只有一种具有switchView,也可以用于所有回收单元。
您还也只为一种类型的单元格设置了TEXTFIELD_TAG
,但是在访问所有类型的“回收”单元格时都要引用它。
编辑添加:我看到您已经从控制台添加了例外。 调用setSecureTextEntry
引发异常。 我在复制并粘贴到问题的代码中的任何地方都看不到setSecureTextEntry
,因此建议您在实际代码中以及无论调用该代码的地方查找setSecureTextEntry
,确保它是接收该调用的UITextField
而不是UITableViewCell
(可以是安全UITextField
所在的超级视图)。
首先,将新控件作为子视图添加到contentView(或单元)中,如下所示:
[cell.contentView addSubview:textField];
...等等其他观点。 查看是否可以解决崩溃问题。
另外,正如@Eugene指出的那样,在单元格上设置标签也无济于事。 在您创建的视图上设置标签。 (尽管我怀疑那是您崩溃的原因)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.