当我在自定义单元格内使用 UItextField 时,以及当我 resignFirstResponder 文本字段时,我发生崩溃,但它不再可见(表视图滚动到窗口外)。我仍然可以找到文本字段,指针继续可访问,它不为空,并且崩溃仅发生在 IOS7 上,在 IOS6 上我没有这个问题。这是一些代码:
The textField
是一个全局变量。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[TableCell alloc] init];
if(indexPath.row == 0)
{
[textField setFrame:CGRectMake(15, 5, cell.frame.size.width-60, cell.frame.size.height)];
textField.textAlignment = NSTextAlignmentLeft;
[textField setBorderStyle:UITextBorderStyleNone];
textField.textColor = [UIColor blackColor];
textField.tag = indexPath.row;
textField.delegate = self;
textField.secureTextEntry = YES;
[textField setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];
textField.textColor = [UIColor whiteColor];
textField.returnKeyType = UIReturnKeyDone;
[textField setAdjustsFontSizeToFitWidth:YES];
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Senha" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
[cell.contentView textField];
}
}
return cell;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
// NSLog(@"text field %@",textField);
// NSLog(@"tfield return: %d",textField.isFirstResponder);
[textField resignFirstResponder];
// [self.view endEditing:YES];
return NO;
}
在 Apple 的帮助下,我已经成功修复了类似的崩溃错误。关键是reuseIdentifer
.
引用来自一封来自文森特·盖博 of 苹果开发者技术支持:
这是 iOS 7 中发生的已知行为更改UITableView
,当细胞不被重复使用时。
这里的修复是确保您遵循正确的单元重复使用。如果您不想重复使用UITableViewCells
,那么建议您简单地将所有视图布局在一个UIScrollView
.
为了确保重复使用单元格,请确保将相同的字符串传递给dequeueReusableCellWithIdentifier:
你传递给reuseIdentifier:
使用时alloc/init
来制作细胞。该字符串不能为零。
所以我认为你应该确保你已经设置TableCell
's reuseIdentifer
与您传递给的值相同的属性dequeueReusableCellWithIdentifier:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)