문제

응용 프로그램에서 다음 방법을 사용하고 있습니다.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
        cell.contentView.alpha = 0.5;
    }
}   

응용 프로그램을 실행하면 테이블에 7 행이 있습니다. 상기 함수에 따라 첫 번째 행 (행 번호 0)의 셀만 (IF 조건으로 인해) 형식화되어야합니다.

1 번째 행의 셀 (행 번호 0)은 원하는 출력에 따라 제대로 형식화됩니다. 그러나 테이블 아래로 스크롤하면 하나 더 셀이 더 형식으로 표시됩니다 : 행 번호 5의 셀.

왜 그렇게?

도움이 되었습니까?

해결책

나는 블라디미르의 대답에 동의합니다. 그러나 나는 또한 당신이 다른 접근법을 따라야한다고 생각합니다.

현재 상황에서는 각 스크롤에서 메소드가 호출되므로 셀을 자주 포맷하고 있으며, 이는 차선책 성능으로 이어집니다.

더 우아한 솔루션은 1 행을 다른 행을 다른 행을 "한 번"과 다르게 포맷하는 것입니다. 셀을 만들 때.

    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier;
        if(indexPath.row == 0)
        CellIdentifier = @"1stRow";
        else
        CellIdentifier = @"OtherRows";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell==nil) { 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            if(indexPath.row == 0){
                cell.contentView.backgroundColor = [UIColor lightGrayColor];  
                cell.contentView.alpha = 0.5;
                    // Other cell properties:textColor,font,...
            }
            else{
                cell.contentView.backgroundColor = [UIColor blackColor];  
                cell.contentView.alpha = 1;
                //Other cell properties: textColor,font...
            }

        }
        cell.textLabel.text = .....
        return cell;
    }

다른 팁

그 이유는 TableView가 이미 존재하는 셀을 재사용하고 가능하다면 표시하기 때문입니다. 여기서 발생하는 일 - 테이블이 스크롤되고 행 0이 보이지 않게되면 새로 표시된 행에 반응하는 셀이 사용됩니다. 따라서 셀을 재사용하는 경우 속성을 재설정해야합니다.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
   if(indexPath.row == 0) { 
       cell.contentView.backgroundColor = [UIColor lightGrayColor];  
       cell.contentView.alpha = 0.5; } 
   else
   {
    // reset cell background to default value
   }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top