繁体   English   中英

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIButton setText:]

[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]

我正在制作一个应用程序并在其中使用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;

   }

问题是你将cell.button.tag设置为indexPath.row ,如果有12行或更多行,你的UIButtons最终可能会得到与你已经标记为11-14的UILabel相同的标签。所以当你打电话给[cell viewWithTag:11];时,偶尔会发生什么? 例如,你是在拔出UIButton而不是UILabel (演员不会改变它),所以你最终试图在该属性不存在时设置UIButton文本 - 因此Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]错误。 我建议您将UILabel标记设置为包含远远超出行数的数字,这样就不存在冲突。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM