簡體   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