[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]
i am making an app and using tableview controller in it and using checkboxes in table view cell. 我正在制作一个应用程序并在其中使用tableview控制器并使用表格视图单元格中的复选框。 all are working fine but when i scroll down tableview it is working fine but when i scroll up tableview then it is giving an exception mentioned above.below is my sample code.
所有工作正常,但当我向下滚动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;
}
and this is me setting cell method 这是我设置细胞方法
-(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;
}
The problem is that you're setting cell.button.tag
to indexPath.row
and if there are 12 or more rows, your UIButtons
s can end up having the the same tag as one of your UILabel
s already tagged 11 - 14. So what's occasionally happening when you call [cell viewWithTag:11];
问题是你将
cell.button.tag
设置为indexPath.row
,如果有12行或更多行,你的UIButtons
最终可能会得到与你已经标记为11-14的UILabel
相同的标签。所以当你打电话给[cell viewWithTag:11];
时,偶尔会发生什么? for example, is that you're pulling out the UIButton
and not the UILabel
(the cast doesn't change that) and so you end up attempting to set the UIButton
text when that property doesn't exist -- thus the Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]
error. 例如,你是在拔出
UIButton
而不是UILabel
(演员不会改变它),所以你最终试图在该属性不存在时设置UIButton
文本 - 因此Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]
错误。 I suggest setting your UILabel
tags to contain numbers far beyond your row count so there's no conflict. 我建议您将
UILabel
标记设置为包含远远超出行数的数字,这样就不存在冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.