2012-06-18 107 views
0

我有Xcode 4.3并且该类不使用ARC。滚动单元格数据更改

我创建了一个使用可重用单元的tableview。我试图从别人那里得到答案,但无法得到适当的答案。我使用UILabel来显示数据,使用UIImageView来显示图像。

我的代码如下。这很长,但想法是创建并放置一次,并更改数据。

在滚动中,只有一个小区改变(滚动时我看到不同的数据),以及其他的再利用细胞显示从小区1

static NSString * CellIdentifier = @"HistoryCellId"; 

iImage = [UIImage imageNamed: [NSString stringWithFormat:@"%@.png",[(_searching ? _titleArrCopy : _titleArr) objectAtIndex:indexPath.row]]]; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier]; 
    iconImage = [[UIImageView alloc] init]; 
    UIImage * image = [UIImage imageNamed:@"HFcellBG.png"]; 
    UIImageView * bgImage = [[UIImageView alloc] initWithImage: image]; 
    cell.backgroundView = bgImage; 
    //[iconImage removeFromSuperview]; 
    iconImage.image = iImage; 
    iconImage.frame = CGRectMake(_iconX, 11, 58, 58); 
    //iconImage.backgroundColor = [UIColor whiteColor]; 
    [cell addSubview:iconImage]; // show the image on the side 

    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_titleX, 3, 212, 25)]; 
    titleLabel.textAlignment = _nibNameRTL ? UITextAlignmentRight : UITextAlignmentLeft; 
    [cell addSubview:titleLabel]; // show a title 

    timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_dateX, 25, 212, 20)]; 
    timeLabel.textAlignment = _nibNameRTL ? UITextAlignmentRight : UITextAlignmentLeft; 
    timeLabel.font = [UIFont systemFontOfSize:15]; // show another data 
    [cell addSubview:timeLabel]; 

    favoriteBtn = [[UIButton alloc] initWithFrame:CGRectMake(_removeX, 48, 66, 23)]; 
    favoriteBtn.titleLabel.font = [UIFont systemFontOfSize:12]; 
    [favoriteBtn setTitle:getTextValue(3) forState:UIControlStateNormal]; 
    [cell addSubview:favoriteBtn]; 
    // show a button 

} 

[iconImage setImage:iImage]; 

[titleLabel setText:[self parserData:indexPath.row]]; 

[timeLabel setText:[NSString stringWithFormat:getTextValue(4),[(_searching ? _TimeArrCopy :_TimeArr) objectAtIndex:indexPath.row]]]; 

if ([[(_searching ? _FavoriteArrCopy : _FavoriteArr) objectAtIndex:indexPath.row] isEqualToString:@"1"]) { 
    [favoriteBtn setEnabled:NO]; 
    [favoriteBtn setBackgroundImage:[UIImage imageNamed:@"HFfavoriteCheckedBtn.png"] forState:UIControlStateNormal]; 
} 
else { 
    [favoriteBtn setEnabled:YES]; 
    [favoriteBtn setBackgroundImage:[UIImage imageNamed:@"HFfavoriteBtn.png"] forState:UIControlStateNormal]; 
} 

favoriteBtn.tag = indexPath.row; 
[favoriteBtn addTarget:self action:@selector(addToFavorite:) 
     forControlEvents:UIControlEventTouchUpInside]; 


// [cell.favoriteBtn setTitle:getTextValue(3) forState:UIControlStateNormal]; 
// [cell.typeImage setImage:iconImage]; 
// cell.data = [_dataArr objectAtIndex:indexPath.row]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
// return it 
return cell; 

}

的数据是像下面的相同的数据:我可以看到,每次5个细胞和滚动,当我看到下面的行为:

1 
2 
3 
4 
5 - the cell 5 changes all the time while scrolling 
1 - cell one data again 
2 
etc 

当我检查indexPath.row,这是一个正确的行。

怎么了?

回答

0

你试图用来配置单元格的变量是在最有可能不是真的if检查(特别是在滚动后,当你的单元格实例已经存在时)初始化的。您应该访问单元对象的属性来修改它的状态。

这是Objective-C的一个缺陷,允许你发送消息给nil。

[titleLabel setText:[self parserData:indexPath.row]]; 

titleLabel在您滚动后将为零,并且不会有错误。

如果您的单元格中有很多自定义用户界面,我建议您制作一个自定义子类,然后为所有需要自定义的子视图添加属性,然后在if(cell == nil )块。

+0

我该如何解决这个问题?以及我应该如何使用子类?我不想要一个单元nix,这会造成很多麻烦。 –

+0

我管理修复它与以下链接:http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview –

相关问题