我正在创建一个应用程序并在其中使用tableview控制器并使用表格视图单元格中的复选框。所有工作正常,但当我向下滚动tableview它工作正常,但当我向上滚动tableview然后它给出了上面提到的异常。下面是我的示例代码。
- (void)viewDidLoad {
[super viewDidLoad];
[checkimageArray removeAllObjects];
[lblArray removeAllObjects];
self.MainView.hidden=YES;
checkBoxesCount=0;
self.lblArray=[NSMutableArray new];
self.image.hidden=YES;
self.checkimageArray=[NSMutableArray new];
for (int i=0; i<15; i++) // when i decrease this loop to 12 it waz working fine but on 15 it is not working
{
[self.lblArray addObject:[NSString stringWithFormat:@"cell Number %d",i]];
}
[self.activitiesTableView_ reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.lblArray.count;
}
这是我设置单元格方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableviewidentifier = @"cell";
tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell==nil)
{
cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}if(indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1){
// [[cell textLabel] setText:@"Load more records"];
}
UILabel *valuedate = (UILabel *)[cell viewWithTag:11];
UILabel *msg = (UILabel *)[cell viewWithTag:12];
UILabel *date = (UILabel *)[cell viewWithTag:13];
UILabel *time = (UILabel *)[cell viewWithTag:14];
[valuedate setText:@"Demo"];
[msg setText:@"How are You?"];
date.text=@"14/07/2014";
time.text=@"A 08:16";
cell.image.layer.borderColor=(__bridge CGColorRef)([UIColor blackColor]);
cell.image.layer.borderWidth=2.0;
valuedate.font=[UIFont fontWithName:@"SegoeUI" size:15];
msg.font=[UIFont fontWithName:@"SegoeUI-light" size:10.0];
date.font=[UIFont fontWithName:@"SegoeUI-light" size:9];
time.font=[UIFont fontWithName:@"SegoeUI-light" size:9];
[cell.button setBackgroundImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
[cell.button setImage:nil forState:UIControlStateNormal];
if ([self.checkimageArray containsObject:[self.lblArray objectAtIndex:indexPath.row]])
{
[cell.button setImage:[UIImage imageNamed:@"tick.png"]
forState:UIControlStateNormal];
cell.backgroundColor=[UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
}
else
{
cell.backgroundColor=[UIColor clearColor];
}
cell.button.tag=indexPath.row;
[cell.button addTarget:self action:@selector(checkButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
答案 0 :(得分:3)
问题在于您将cell.button.tag
设置为indexPath.row
,如果有12行或更多行,您的UIButtons
最终可能会拥有与{1}相同的标记您的UILabel
已标记为11 - 14.例如,当您致电[cell viewWithTag:11];
时偶尔发生的事情是,您是否正在撤出UIButton
而不是UILabel
(演员阵容不会更改),因此当该属性不存在时,您最终会尝试设置UIButton
文字 - 因此Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]
错误。我建议您将UILabel
代码设置为包含远远超出行数的数字,这样就不存在冲突。