Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么上下滚动UITableView会使我的UIButton标题颜色发生变化_Ios_Iphone_Uitableview_Uibutton - Fatal编程技术网

Ios 为什么上下滚动UITableView会使我的UIButton标题颜色发生变化

Ios 为什么上下滚动UITableView会使我的UIButton标题颜色发生变化,ios,iphone,uitableview,uibutton,Ios,Iphone,Uitableview,Uibutton,我在表格视图单元格中添加了一个UIButton,当我点击该按钮时,它会将按钮的标题颜色设置为灰色,并将数据源的watched属性设置为true,默认值为false。因此,当一行的观察值等于true时,UIButton的标题应为灰色,如果不是,则应为默认颜色蓝色。正如您在下面看到的,一切都很完美,但如果我点击一个UIButton,文本颜色将变为灰色,并且它的数据源也会发生变化。但如果我上下滚动tableview,其他单元格的按钮文本颜色也会变为灰色 - (UITableViewCell *)tab

我在表格视图单元格中添加了一个UIButton,当我点击该按钮时,它会将按钮的标题颜色设置为灰色,并将数据源的watched属性设置为true,默认值为false。因此,当一行的观察值等于true时,UIButton的标题应为灰色,如果不是,则应为默认颜色蓝色。正如您在下面看到的,一切都很完美,但如果我点击一个UIButton,文本颜色将变为灰色,并且它的数据源也会发生变化。但如果我上下滚动tableview,其他单元格的按钮文本颜色也会变为灰色

- (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];
    }
}
您必须将颜色设置回原始颜色,因为tableview将重用灰色单元格

if ([eposide.watched boolValue]) {
    [eCell.number setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}else{
    [eCell.number setTitleColor:yourOriginColor forState:UIControlStateNormal];
}