我正在创建一个包含三种不同类型的自定义单元格的表格,其中一个,一个自定义单元格有UITextField
。我使用此自定义单元格创建两行。我为两行的文本字段设置了标记和委托。
我的问题是,当我滚动表格时,带有文本字段的这些行向上移动并从屏幕上消失,向下滚动时,我的应用程序崩溃了。
我收到了像
-[CellImageViewController txtField]: unrecognized selector sent to instance 0xa0ea5e0
以下是cellForRowAtIndexPath:
if (indexPath.row == 0 )
{
if (cell == nil) {
cell = [[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
}
cell.txtField.tag =1;
cell.txtField.delegate = self;
cell.txtField.text = @"kjfkd";
}
return cell;
}
else if(indexPath.row==1)
{
if (cell == nil) {
cell = [[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
}
cell.txtField.tag = 2;
cell.txtField.text = @"glk";
cell.txtField.delegate = self;
return cell;
}
有人对这个问题有所了解吗?
答案 0 :(得分:1)
您有不同类型的单元格,因此您需要为每个单元格使用不同的单元格标识符。
试试这个:
customOrNormalCell *cell [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
if(!cell){
cell = [[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%i", indexPath.row]];
}
使用此代码,每个cellIdentifier将为1,2,3,4 ......(全部不同)
我很确定它会解决问题
答案 1 :(得分:0)
在创建UITextField
之前创建UITableView
并在单元格内容视图中添加Textfield对象
[cell.contentView addSubview:yourTxtFieldOblect];
答案 2 :(得分:0)
看起来您可能正在重复使用其他类型的单元格。 在尝试访问它的属性之前,请尝试确定要重用的单元格类型。
答案 3 :(得分:0)
为什么要为每一行定义单元格?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
CellWithTextFieldViewController *cell = (CellWithTextFieldViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell =[[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
cell.txtField.tag = indexPath.row;
cell.txtField.text = [SET TEXT FROM ARRAY];
cell.txtField.delegate = self;
}
}
答案 4 :(得分:0)
我在项目中也遇到过这个问题。
试试这个:
禁用Tableview中的滚动属性并创建一个scrollview。然后在滚动视图中添加Tableview。
UITableView *table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 976, 395) style:UITableViewStylePlain];
[table setSeparatorStyle:UITableViewCellSelectionStyleNone];
UIScroll *scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(24, 168, 978, 395)];
[table setScrollEnabled:NO];
[scroll addSubview:table];
[self.view addSubview:scroll];
[scroll sendSubviewToBack:table];
答案 5 :(得分:0)
试试这个
static NSString *cellIdentifier= @"Cell";
CellWithTextFieldViewController *cell = (CellWithTextFieldViewController *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.txtField.tag =indexPath.row+1;
cell.txtField.delegate = self;
cell.txtField.text = @"kjfkd";
return cell;
答案 6 :(得分:0)
滚动tableView时,您的单元格正在重复使用。这就是为什么textField正在消失。尝试为不同的自定义单元格使用不同的单元格ID。别忘了在nib文件中为单元标识符
提供相同的id- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customCellOne = @"customCell1";
static NSString *customCellTwo = @"customCell2";
UITableViewCell *cell =nil;
if (0 == indexPath.row)
{
cell = (CustomCellOne *)[tableView dequeueReusableCellWithIdentifier:customCellOne];
if (nil == cell)
{
// Create custom cell one and do what ever you want
}
}
else if (1 == indexPath.row)
{
cell=(CustomCellTwo *)[tableView dequeueReusableCellWithIdentifier:customCellTwo];
if (nil == cell)
{
// Create custom cell two and do what ever you want
}
}
return cell;
}
答案 7 :(得分:0)
我不知道,可能是在重复使用单元格时,文本域委托被释放了。 可能你可以设置
textfield.deleagate = self;
在CellWithTextFieldViewController.m
中