2014-10-18 39 views
1

所以我有一个很奇怪的问题。我的代码在iOS 8上效果很好,但不在iOS 7上,我找不出原因。iOS 7上的选择后UITableViewCell没有更新

我有一个tableview有一个项目列表,当您选择一个项目时,勾选标记将被添加到该项目,如果再次选中该项目,则删除选中标记。很简单的权利? :-像我说的那样,它在iOS 8上效果很好,但是当我在iOS 7.1上运行时,单元格突出显示,添加了一个复选标记,并删除旧标题并将其替换为默认标题。之后,无论我点击单元格多少次,它都不会改变(但底层数据确实会改变)。

选择 Before Selection

之前选择 After Selection

如果我离开屏幕,并回到它之后,正确显示的行。我已经验证了cellForRowAtIndexPath被调用并且正确的值被添加到单元格中。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [[parkFinderSingleton.data valueForKey:@"amenities"] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"amenityFilterCell" forIndexPath:indexPath]; 

    Amenity *currentAmenity; 
    NSArray *amenities = [parkFinderSingleton.data valueForKey:@"amenities"]; 


    if (amenities != nil) { 
     currentAmenity = amenities[indexPath.row]; 
     cell.textLabel.text = currentAmenity.amenityTitle; 
     NSLog(@"Cell Title %@", cell.textLabel.text); 

     if (currentAmenity.amenitySelected) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } else { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"amenityFilterCell" forIndexPath:indexPath]; 

    Amenity *currentAmenity; 
    NSArray *amenities = [parkFinderSingleton.data valueForKey:@"amenities"]; 

    if (amenities != nil) { 
     currentAmenity = amenities[indexPath.row]; 

     if (currentAmenity.amenitySelected) { 
      currentAmenity.amenitySelected = NO; 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } else { 
      currentAmenity.amenitySelected = YES; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
    } 

    [self.tableView beginUpdates]; 
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
    [self.tableView endUpdates]; 

} 

有关可能发生什么的任何想法?

回答

2

通常,dequeueReusableCellWithIdentifier:forIndexPath:不应在tableView: didSelectRowAtIndexPath:中调用。如果您希望单元格用于特定的索引路径,请使用[tableView cellForRowAtIndexPath:indexPath]

为:

[self.tableView beginUpdates]; 
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
[self.tableView endUpdates]; 

如果你只是想取消选择一个单元格,使用[tableView deselectRowAtIndexPath:animated:]

相关问题