2012-01-25 5 views
0

좋아, 다른 UITableView 문제가 있습니다. 어떤 이유로 indexPath.row가 모두 뒤죽박죽입니다. 셀을 설정하는 if 문을 주석 처리하면 모든 것이 잘 동작합니다. NSLogs는 순서대로로드 중이라고 알려주지 만 모든 셀은 순서가 잘못되었습니다.UITableViewCells가 깨졌습니다.

또한 반복되는 것처럼 보입니다. 나는 8 개의 세포 만 보았고 반복해서 반복한다.

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

static NSString *CellIdentifier = @"Cell"; 
NSLog(@"row: %d",indexPath.row); 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.contentView.backgroundColor = [UIColor clearColor]; 

    // Add subviews like this: 
    // [[cell contentView] addSubview:objectName]; 
    // And I get the row number like this: indexPath.row when getting objects from the array 

} 

return cell; 
} 

답변

1

이 코드를 사용하려면 : 여기

내 코드의

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 
NSLog(@"row: %d",indexPath.row); 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.backgroundColor = [UIColor clearColor]; 
     cell.contentView.backgroundColor = [UIColor clearColor]; 

     // Add subviews like this: 
     // [[cell contentView] addSubview:objectName]; 


    } 
    ### Move this here ### 
// And I get the row number like this: indexPath.row when getting objects from the array 
return cell; 
} 

"나는 단지 8 개 세포를보고, 그들은 반복 반복합니다." 옳은.

당신이 놓치고 간 것이 그것이 어떻게 작동해야하는지입니다. 그래서 셀이 0 인 경우에만 새 셀을 초기화하는 &을 할당합니다. 그래서 당신은 할당하고 초기화하고 색상을 설정하고 if 문에 하위 뷰를 추가합니다. 그런 다음 if(cell==nil) 다음에 변수가 전달 된 일부 데이터로 채울 유효한 셀이 있음을 알고 있습니다.

이제는 셀을 설정하지 않고 표시된 모든 데이터를 할당하는 것이 문제입니다 indexPath에 따라. 문제는 셀이 두 번째로 사용되지 않으므로 데이터가 변경되지 않는다는 것입니다.

속도 덧글에 더 자세히 설명하기 위해 이전 폴백 예를 사용합니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     UILabel *hugeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 
     hugeLabel.tag = 300; 
     [cell addSubview:hugeLabel]; 
    } 
    [(UILabel *)[cell viewWithTag:300] setText:[arrayOfStrings objectAtIndex:indexPath.row]]; 
    return cell; 
} 

위의 예제에서 보면, 우리가 그것을 설정 셀에 UILabel를 추가 것을 볼 300 태그의 것 그런 후 문 우리는 새로운 세포 또는 재사용 세포 중 하나를 사용할 경우에 레이블에 텍스트가 이미 있습니다. 어떠한 방법 으로든 우리는 단순히 기존 레이블의 텍스트를 행을 고려해야하는 것으로 변경합니다. 이 방법으로 우리는 반복적 인 견해를 만드는 것을 피합니다.

는 캐시에 죽은 설정 당신이하는 경우 UITableViewCells이 같은 할 수있는 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row < _cells.count){ 
     return [_cells objectAtIndex:indexPath.row]; // _cells is an NSMutableArray setup in viewDidLoad 
    } 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""]; 
    cell.textLabel.text = [source objectAtIndex:indexPath.row]; // source is an NSArray of NSStrings I set up in viewDidLoad 
    [_cells addObject:cell]; 
    return cell; 
} 

을 효율적 무엇 콘솔에서 당신이 Received memory warning을 볼 때 장치에서이 놀라지 않는 실행하는 경우 & 쉬운 것은 종종 동일하지 않습니다.

+0

알겠습니다. 감사합니다. 그것을 약간 수정하고 일하는 것 같습니다. 하지만 꽤 많은 하위 뷰를 추가하기 때문에로드 시간을 늘리는 방법에 대해 알고 있습니다. 저장 한 경우 하위 뷰를 저장하고 다시로드하는 것이 좋습니다. 생각? – iosfreak

+0

NSLog를 셀 == nil에 추가하면 8 번만 나타납니다. 내가 아래로 스크롤했을 때조차도, 그것들을 반복하고 있습니다. – iosfreak

+0

큐 해제 프로세스는 테이블 뷰 스크롤을 빠르게합니다. 네가 반복해서 반복 한 세포가 8 개 정도 있다고했다. 이러한 tableviewcell은 한 번만 생성되기 때문에 좋습니다.tableview의 dequeue 메소드는 새로운 tableviewcell을 만드는 대신 내용을 변경하고 다시 표시 할 수 있도록 화면 밖으로 스크롤 한 tableviewcell을 제공합니다. – NJones

0

, cell.backgroundColorcell.contentView.backgrounColor 등의 설정 방법은 if (cell == nil) 일 때만 설정됩니다. 이 코드를 if 문 블록 외부로 이동해야 dequeueReusableCellWithIdentifier:이 셀을 생성 할 때와 인벤토리에 셀이없고 아무것도 생성하지 않을 때 (즉, nil) 모두 호출 될 수 있습니다.

관련 문제