2012-06-18 2 views
0

Xcode 4.3이 있고 그 클래스는 ARC를 사용하지 않습니다.스크롤 할 때 셀 데이터가 변경됩니다.

재사용 가능한 셀을 사용하는 테이블 뷰를 만들었습니다. 나는 다른 사람들에게서 해답을 얻으려고했지만 적절한 것을 얻을 수 없었다.

내 테이블은 배열에서 데이터를 가져오고 데이터를 표시하려면 UILabel을 사용하고 이미지를 표시하려면 UIImageView을 사용합니다.

내 코드는 다음과 같습니다. 길기는하지만 UILabel 버튼을 만들고 이미지를 한 번 변경하고 데이터를 변경하는 것이 좋습니다. 스크롤 방식

, 하나의 셀 변경 (스크롤하는 동안 다른 데이터 참조), 다른 재사용 셀은 셀 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은 스크롤 한 후 아무 것도 표시되지 않으므로 오류가 발생하지 않습니다.

셀에 많은 사용자 지정 UI가있는 경우 사용자 지정 하위 클래스를 만들고 사용자 지정해야하는 모든 하위 뷰의 속성을 추가 한 다음 if (셀 == nil) 블록.

+0

어떻게 해결할 수 있습니까? 하위 클래스는 어떻게 사용해야합니까? 나는 세포 nix를 원하지 않는다, 그것은 많은 문제를 만든다. –

+0

다음 링크로 문제를 해결했습니다 : http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview –

관련 문제