开发者

app crashes when i try to scroll the tableview

开发者 https://www.devze.com 2023-04-12 12:46 出处:网络
when i try to scroll my tableview it crashes the app,below shown is my code please help me to fix this issue

when i try to scroll my tableview it crashes the app,below shown is my code please help me to fix this issue

-(UITableViewCell*)tableView:(UITableView开发者_StackOverflow社区*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    NSLog(@"indexPath.row---%d",indexPath.row);
    UILabel *skuLbl;
//   *cellImgView;
    static NSString *cellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil){
        NSLog(@"indexPath cell");
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
        UIImageView *cellImgView=[[UIImageView alloc]initWithFrame:CGRectMake(25,20,717,125)];
        cellImgView.image=[UIImage imageNamed:@"cell.png"];
        [cell.contentView addSubview:cellImgView];
        skuLbl=[[UILabel alloc]initWithFrame:CGRectMake(62,20,680,125)];
        skuLbl.backgroundColor=[UIColor clearColor];
        skuLbl.font=[UIFont fontWithName:@"helvetica" size:40];
        skuLbl.textColor=[UIColor blackColor];
        [cell.contentView addSubview:skuLbl];
        cell.selectionStyle=NO;    
        tableView.separatorColor=[UIColor whiteColor];
    }



//  NSLog(@"inside cell--%@",[[appDelegateObj.parsedRowsSku objectAtIndex:indexPath.row]objectForKey:@"SKU"]);
//  skuLbl.text=[[appDelegateObj.parsedRowsSku objectAtIndex:indexPath.row]objectForKey:@"SKU"];
    skuLbl.text=[NSString stringWithFormat:@"%d",indexPath.row];
    return cell;

}


When you scroll your table view it tries to reuse its cells so

 if(cell==nil){        
    ...
 }

may not get called and your skuLbl pointer remains not initialized. So later you're trying to access that invalid pointer and that results in crash.

You need to get reference to label outside of initialization block to make sure it is always valid:

UILabel *skuLbl = nil;
static NSString *cellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
    ...
    skuLbl.tag = 100;
    ...
}
skuLbl = (UILabel*)[cell.contentView viewWithTag:100];
...


make sure your number of rows are equals to the size of your array or whatever you're using with this method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  return [self.myArray count]; }


Edit your code as below::

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    UILabel *skuLbl = nil;
    static NSString *cellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil) {
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];

        UIImageView *cellImgView=[[UIImageView alloc]initWithFrame:CGRectMake(25,20,717,125)];
        cellImgView.image=[UIImage imageNamed:@"cell.png"];
        [cell.contentView addSubview:cellImgView];

        skuLbl=[[UILabel alloc]initWithFrame:CGRectMake(62,20,680,125)];
        skuLbl.tag = 100;
        skuLbl.backgroundColor=[UIColor clearColor];
        skuLbl.font=[UIFont fontWithName:@"helvetica" size:40];
        skuLbl.textColor=[UIColor blackColor];
        [cell.contentView addSubview:skuLbl];

        cell.selectionStyle=NO;    
        tableView.separatorColor=[UIColor whiteColor];
    } else {
       // This method called at creation and scrolling of tableview for every individual cell. 
       // Due to creation and scrolling of tableview skuLbl refrence get nil
       // So get new refrence of skuLbl from cell's contentView as below
       skuLbl = (UILabel*)[cell.contentView viewWithTag:100];
    }

    skuLbl.text=[NSString stringWithFormat:@"%d",indexPath.row];
    return cell;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号