当我尝试滚动tableview时app崩溃了

时间:2011-10-10 13:46:31

标签: objective-c ios uitableview

当我尝试滚动我的tableview时会崩溃应用程序,下面显示的是我的代码请帮我修复此问题

-(UITableViewCell*)tableView:(UITableView*)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;

}

3 个答案:

答案 0 :(得分:4)

当您滚动表格视图时,它会尝试重复使用其单元格

 if(cell==nil){        
    ...
 }
可能无法调用

并且您的skuLbl指针仍未初始化。因此,稍后您将尝试访问该无效指针,从而导致崩溃。

您需要在初始化块之外引用标签以确保它始终有效:

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

答案 1 :(得分:0)

确保您的行数等于数组的大小或此方法使用的任何行:

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

答案 2 :(得分:0)

编辑您的代码,如下所示::

-(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;

}