简体   繁体   中英

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. 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.

- (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]; 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. I suggest setting your UILabel tags to contain numbers far beyond your row count so there's no conflict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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