2016-10-22 4 views
1

저는 맞춤 셀이 있습니다. cellForRowAtIndexPath 방법에서는 사용자 정의 UILabel으로 indexPath.row 번호를 표시하고 cellForRowAtIndexPath에 정의되어 있습니다. 해당 레이블은 해당 사용자 정의 셀의 하위보기로 추가됩니다. 셀 수가 많고 테이블 뷰를 아래로 스 와이프하면 indexPath.row 숫자가 이상한 값을 가지며 더 스크롤하면 값이 커집니다. 어떻게 이것을 무시할 수 있습니까?indexPath.row는 이상한 값을 제공합니다.

[편집] 내 코드 : cellForRow 방법의 새로운 UIButtons와 UILabels을 만드는

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"SKSTableViewCell"; 

    self.sksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!self.sksTableViewCell) 
     self.sksTableViewCell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    self.sksTableViewCell.textLabel.text = [NSString stringWithFormat:@" %@",[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] name]]; 
    self.sksTableViewCell.backgroundColor = [UIColor clearColor]; 

    if ([[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] times] count]) { 
     self.sksTableViewCell.expandable = YES; 
    } else { 
     self.sksTableViewCell.expandable = NO; 
    } 

    // UIButton for directly switching racers 
    UIButton *stopwatchIcon = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [stopwatchIcon addTarget:self action:@selector(pressedButtonToGoToTheRacerDirectly:) forControlEvents:UIControlEventTouchUpInside]; 
    UIImage *img = [UIImage imageNamed:@"stopwatch"]; 
    stopwatchIcon.frame = CGRectMake(self.view.bounds.size.width-70, 27.5-img.size.height/2, img.size.width, img.size.height); 
    [stopwatchIcon setImage:img forState:UIControlStateNormal]; 
    stopwatchIcon.tag = [[self.indexDictionary valueForKey:[NSString stringWithFormat:@"%@", [[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] name]]] intValue]; 
    [self.sksTableViewCell addSubview:stopwatchIcon]; 

    // Medals 
    int golds = [[self.bestThreeRacersDictionary objectForKey:@"golds"] intValue]; 
    int silvers = [[self.bestThreeRacersDictionary objectForKey:@"silvers"] intValue]; 
    int bronzes = [[self.bestThreeRacersDictionary objectForKey:@"bronzes"] intValue]; 

    // Medal image and view 
    UIImage *medalImage = [UIImage imageNamed:@"medal"]; 
    UIImageView *medalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(stopwatchIcon.frame.origin.x-medalImage.size.width-7.5, 27.5-medalImage.size.height/2, medalImage.size.width, medalImage.size.height)]; 
    // TextLabel 
    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(medalImageView.frame.origin.x+medalImageView.frame.size.width/4, medalImageView.frame.origin.y+medalImageView.frame.size.height/4, medalImageView.frame.size.width/2, medalImageView.frame.size.height/2)]; 
    numberLabel.backgroundColor = [UIColor clearColor]; 
    numberLabel.textColor = [UIColor whiteColor]; 
    numberLabel.textAlignment = NSTextAlignmentCenter; 
    numberLabel.adjustsFontSizeToFitWidth = YES; 
    numberLabel.font = [UIFont systemFontOfSize:12]; 
    numberLabel.text = [NSString stringWithFormat:@"%i", [Racer shift:golds silvers:silvers bronzes:bronzes]+helpIndex]; 
    helpIndex++; 
    if (indexPath.row < golds) { 
     medalImage = [UIImage imageNamed:@"goldMedal"]; 
     numberLabel.text = @"1"; 
     helpIndex--; 
    } else if (indexPath.row < (golds+silvers)) { 
     medalImage = [UIImage imageNamed:@"silverMedal"]; 
     numberLabel.text = @"2"; 
     helpIndex--; 
    } else if (indexPath.row < (golds+silvers+bronzes)) { 
     medalImage = [UIImage imageNamed:@"bronzeMedal"]; 
     numberLabel.text = @"3"; 
     helpIndex--; 
    } 
    medalImageView.image = medalImage; 
    medalImageView.tag = 5; 
    numberLabel.tag = 5; 
    [self.sksTableViewCell addSubview:medalImageView]; 
    [self.sksTableViewCell addSubview:numberLabel]; 

    return self.sksTableViewCell; 
} 
+0

코드를 보여줄 수 있습니까? –

+0

아마도'cellForRow'가 호출 될 때마다 새 레이블을 추가 할 것입니까? 그렇다면 그렇게하지 말아야합니다. – alexburtnik

+1

왜 마지막 셀에 대한 참조를 유지하기 위해 속성을 사용하고 있습니까? – rmaddy

답변

1

마십시오. UITableView의 셀은 다시 사용되므로 스크롤 할 때 화면에서 사라지는 객체가 다시 사용되어 새 행이 표시됩니다. 그리고 이런 일이 발생할 때마다 새로운 시각을 추가하고 있습니다.

대신 모든보기 계층을 한 번만 만들고 업데이트해야하는보기를 업데이트해야합니다.

if (!cell) { 
    cell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    //create all your subviews here 
} 

//update subviews here 
1

모든 셀에 대해 UI 구성 요소의 새 개체를 만들지 말고 셀 재사용 후에 한 번만 수행하십시오.

관련 문제