Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

ios - UITableViewCell reusability issue. Modifying one cell is affecting others

Whenever a cell from section 3 is selected. I'm updating the DataSource array, and the cell's background color is thus changing correctly.

However, whenever I scroll back up I start seeing random cells with the modified background color, knowing that I don't even mention it in my cellForRowAtIndexPath method and each section in my tableView is populated from a separate DataSource Array/Dictionary!

(I'm using a Storyboard to handle all UI setup)

Here's my code (focus on Section 3)

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

ECTextFieldTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:kSettingsTextFieldCellReuseIdentifier
                                                                  forIndexPath:indexPath];

if (indexPath.section == 0) {

    cell.cellTextField.text = [self.personalInfoDataSource valueForKey:kUserValuesKey][indexPath.row];

 } else if (indexPath.section == 1) {

     cell.cellTextField.text = [self.contactInfoDataSource valueForKey:kUserValuesKey][indexPath.row];

 } else if (indexPath.section == 2) {

     cell.cellTextField.text = [self.professionalDetailsDataSource valueForKey:kUserValuesKey][indexPath.row];

 } else if (indexPath.section == 3) { //---- Problems here 

     UserMeta *metaObj = self.interestDataSource[indexPath.row];

     cell.cellTextField.userInteractionEnabled = NO;
     cell.cellTextField.text = metaObj;

     if (self.user.INTEREST.count > 0 && [self.user.INTEREST contains:metaObj.name] ) {
         cell.backgroundColor = [UIColor redColor];
     }         
 }

 return cell;
}

And here's where I'm doing all the DataSource modifications

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.section == 0 && indexPath.row == 2) {

    // Do Stuff

} else if (indexPath.section == 3) { //---- Problems here 

    ECTextFieldTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    cell.backgroundColor = [UIColor redColor];

    UserMeta *metaObj = self.interestDataSource[indexPath.row];
    [self.user.INTEREST addObject:metaObj];

} 

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you wrote, cells are reused. A cell which was displayed in section 3 could be reused in section 0.

Therefore you have to make sure that all parameters are set to a defined state.

That means if you set userInteractionEnabled to NO and the background color depending on a condition to red in section 3 you have to set userInteractionEnabled to YES and the color to the default color in all other sections. Further you have to set the color to the default color in section 3 if the condition is false.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...