Я добавил UIButton в свою ячейку просмотра таблицы, и когда я нажимаю кнопку, он устанавливает цвет кнопки buttonColor в серый цвет и устанавливает мой При просмотре свойства datasource значение true, значение по умолчанию - false. Итак, когда наблюдаемая строка равна true, заголовок UIButton должен быть серым, если нет, то это должен быть цвет по умолчанию, синий. Как вы можете видеть ниже, все работает отлично, но если я нажму UIButton, цвет текста изменится на серый, и его источник данных тоже изменится. Но если я прокручиваю свой табличный вид вверх и вниз, цвет текста кнопки другой ячейки меняется и на серый.Почему прокрутка UITableView вверх и вниз испортит мне цвет заголовка UIButton, который я изменил
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
EposideCell *cell = (EposideCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[EposideCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.number addTarget:self action:@selector(eposideWatched:) forControlEvents:UIControlEventTouchUpInside];
E *eposide = self.eposides[indexPath.section][@"eposides"][indexPath.row];
cell.eposideTitle.text = eposide.title;
[cell.number setTitle:[eposide.episode stringValue] forState:UIControlStateNormal];
return cell;
}
- (void)eposideWatched:(UIButton *)sender
{
CGPoint buttonPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPoint];
EposideCell *cell = (EposideCell *)[self.tableView cellForRowAtIndexPath:indexPath];
//ignore a lot of core data fetch request code
findE.watched = [NSNumber numberWithBool:![findE.watched boolValue]];
[self.managedObjectContext dct_saveWithCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
[cell.number setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.tableView reloadData];
});
}
}];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
EposideCell *eCell = (EposideCell *)cell;
E *eposide = self.eposides[indexPath.section][@"eposides"][indexPath.row];
if ([eposide.watched boolValue]) {
[eCell.number setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
}