Section1中的UITableViewCells被section1

时间:2015-08-05 18:48:43

标签: ios objective-c uitableview

我有2个部分。第0节获取fetch后的行数。第1节行定义为2。

获取第0部分的数据后,当我重新加载表时,第0部分的前两个单元格与第1部分的两行完全相同。是什么给出了?

enter image description here

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows = 0;
    switch (section) {
       case 0:
        rows = self.subscriptions.count;
        break;
    case 1:
        rows = 2;
        break;
    default:
        break;
}
return rows;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [![enter image description here][1]][1]TTEmailPreferencesCell *cell = [tableView dequeueReusableCellWithIdentifier:TTEmailPreferencesCellIdentifier forIndexPath:indexPath];

switch (indexPath.section)
{
    case 0:
    {
        TTSubscription *subscription = self.subscriptions[indexPath.row];
        [self configureCell:cell withSubscription:subscription];
        [cell.subscriptionSwitch addTarget:self action:@selector(subscriptionSwitchToggled:) forControlEvents:UIControlEventValueChanged];
        break;
    }
    case 1:
    {
        if (indexPath.row == 0) {
            [self configureCell:cell mainTitle:TTNotificationPrefCell1MainTitle descriptionText:TTNotificationPrefCell1Description withSubscription:[self.pushNotificationsDict objectForKey:@"1_pref"]];
            [cell.subscriptionSwitch addTarget:self action:@selector(notificationSwitchToggled:) forControlEvents:UIControlEventValueChanged];
        }
        if (indexPath.row == 1) {
            [self configureCell:cell mainTitle:TTNotificationPrefCell2MainTitle descriptionText:TTNotificationPrefCell2Description withSubscription:[self.pushNotificationsDict objectForKey:@"2_pref"]];
            [cell.subscriptionSwitch addTarget:self action:@selector(notificationSwitchToggled:) forControlEvents:UIControlEventValueChanged];
        }
        break;
    }
    default:
        break;
}

return cell;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.numberOfRows = 0;
    [self.view addSubview:self.tableView];

    [self.tableView registerNib:[UINib nibWithNibName:@"TTEmailPreferencesCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:TTEmailPreferencesCellIdentifier];

 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save", nil) style:UIBarButtonItemStyleDone target:self action:@selector(save:)];

self.modified = NO;


__weak typeof(self) weakSelf = self;
[[TTAPIManager sharedManager] fetchSubscripWithSuccess:^(RKObjectRequestOperation *operation, NSArray *subscriptions) {
    weakSelf.subscriptions = subscriptions;
    [self getNotificationPreferences];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    //Do something
}];

}

2 个答案:

答案 0 :(得分:0)

您的单元格最有可能被出列并重复使用,因此标签可能会被缓存。您是否覆盖了prepareForResuse以将单元格返回到开始状态?

答案 1 :(得分:0)

解决方案原来是在prepareForReuse函数中。来自第1节的2个单元格在[tableView reloadData]调用时出列。 prepareToReuse将所有内容设置为零。将标签设置为空(@“”)并删除交换机的目标/操作就可以了。

- (void)prepareForReuse
{
    [super prepareForReuse];
    self.nameLabel.text = @"";
    self.descriptionLabel.text = @"";
    [self.subscriptionSwitch removeTarget:nil action:NULL forControlEvents:UIControlEventValueChanged];
}