2014-11-24 2 views
0

UITableViewController 기반입니다. 내 표, 두 부분으로 구성UITableViewCell 잘못된 콘텐츠 표시

(1) 어레이에서 프리스트 부 표시 객체 라벨 텍스트는 검정 및 세부 할 텍스트가

(2) 제 2 부분은 하나의 행을 갖고 사용 새로운 ViewController를 사용하여 더 많은 객체 추가하기

그러나 초기 뷰는 정확합니다. 그러나 배열에 객체를 추가하고 데이터를 다시로드하려고하면 테이블에 잘못된 셀에 대한 잘못된 내용이 표시됩니다.

아무도 내가 뭘 잘못했는지 말해 줄 수 있습니까?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) return self.addItems.count; 
    else return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Add Items List"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier 
                  forIndexPath:indexPath]; 

    if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:cellIdentifier]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    if (indexPath.section == 0) { 
    Item *item = [self.addItems objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%08i", item.identifier.intValue]; 
    cell.detailTextLabel.text = item.detailDescription; 
    } 
    else { 
    cell.textLabel.text = @"Create Item"; 
    cell.textLabel.textColor = [tableView tintColor]; 
    cell.detailTextLabel.text = nil; 
    } 

    return cell; 
} 

- (void)unwindToAddItemsViewController:(UIStoryboardSegue *)segue { 
    CreateItemViewController *source = [segue sourceViewController]; 
    [self.addItems addObjectsFromArray:source.createItems]; 
    [self.tableView reloadData]; 
} 
+0

이 메소드의 용도는 무엇입니까 'unwindToAddItemsViewController'입니까? –

+0

두 셀 모두에 대해 동일한 셀을 사용할 계획이라면 셀의 모든 뷰를 원래 상태로 재설정하는 셀에 리셋 메소드가 있습니다. 셀을 다시 사용할 것입니다. – GoodSp33d

답변

0

"잘못된 콘텐츠"는 첫 번째 섹션에있는 셀의 textLabel의 색이 잘못되었음을 나타냅니다. 맞습니까?

section == 0 코드 경로에 textLabel의 textColor를 설정하지 않았기 때문입니다. 셀은 재사용되므로 동일한 재사용 식별자를 사용하면 모든 코드 경로에서 속성을 설정해야합니다. 섹션 1의 셀은 일단 설정되면 레이블의 textColor를 유지합니다. 따라서 해당 셀이 섹션 0 셀로 다시 사용되면 해당 섹션에서 원하는 값으로 다시 변경해야합니다.

그리고 dequeueReusableCellWithIdentifier:forIndexPath:을 사용하여 셀을 대기열에서 제외하기 때문에 if (!cell) 부분을 제거 할 수 있습니다. 이 메소드는 결코 nil을 반환하지 않습니다.

코드는 다음과 같아야합니다

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Add Items List"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier 
                  forIndexPath:indexPath]; 

    if (indexPath.section == 0) { 
    Item *item = [self.addItems objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%08i", item.identifier.intValue]; 

    cell.textLabel.textColor = [UIColor blackColor]; // NEW 

    cell.detailTextLabel.text = item.detailDescription; 
    } 
    else { 
    cell.textLabel.text = @"Create Item"; 
    cell.textLabel.textColor = [tableView tintColor]; 
    cell.detailTextLabel.text = nil; 
    } 
    return cell; 
} 
+0

고마워, 그게 내 문제를 해결. 그래서 그것이 어떻게 작동하는지입니다. 이제이 dequeue에 대해 더 많은 것을 이해합니다. 재사용 가능한 것들!. 하지만 여전히 문제가 있습니다. "잘못된 콘텐츠"를 표시하면 색상이 잘못되었음을 의미하며 클릭 할 때까지는 자막이 표시되지 않습니다. 이제 색상이 맞지만 클릭 할 때까지 여전히 자막이 표시되지 않습니다. –

0

감사합니다, 내 문제를 해결. 그래서 그것이 어떻게 작동하는지입니다. 이제이 dequeue에 대해 더 많은 것을 이해합니다. 재사용 가능한 것들!.

하지만 여전히 문제가 있습니다. '잘못된 콘텐츠'를 표시하면 색상이 잘못되었음을 의미하며 클릭 할 때까지는 자막이 표시되지 않습니다. 제목 아래에는 자막을위한 공간이 있지만, 클릭 할 때까지는 공백입니다.

이제 색상이 맞지만 부제는 클릭 할 때까지 표시되지 않습니다.

관련 문제