故事板中UITableViewController中的错误

时间:2013-11-13 21:00:06

标签: objective-c storyboard uitableview

我正在创建一个UITableViewController来列出人名和他们名字旁边的一颗星,以表示喜欢这样的人

one of many cells

触摸时星星点亮,表示收藏,该单元格的行号进入NSMutableArray,在此方法中调用。

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

当我点击一个单元格并将索引添加到数组时,一切正常,直到我向下滚动,并且更多的星星被填满。我相信它们是随机的,每当我向上滚动然后向下滚动时,会出现一些弹出窗口,看起来像这样,褪色的星星......

faded star

这是全明星

full star

不知何故,不应该填充的星星会消失。

我无法确定星星开关的位置。日志仅显示滚动到特定单元格时将星形设置为开。

我的问题是星星在它们不应该打开时打开,我的阵列很好,我已经多次检查过,它必须是UITableView。

这是我的代码,

我只有两张该星的图像,一张是满的,一张是空的,以及

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //    NSLog(@"%i",[[cell.contentView subviews] count]);
    //    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[cell.contentView subviews]];
    [tableView reloadData];
    UIImageView *star = cell.star;
    NSLog(@"Star Tag: %i",star.tag);

    if (star.tag == kStarEmpty) {
        [[Global sharedGlobal].favTeachers addObject:[NSString stringWithFormat:@"%i",cell.identifierTag]];
        NSLog(@"Added: %i",cell.identifierTag);
     //   NSLog(@"setting star image: 1");
        [star setImage:[UIImage imageNamed:@"star.png"]];
        [star setTag:kStarFilled];
    } else if (star.tag == kStarFilled) {
        [[Global sharedGlobal].favTeachers removeObject:[NSString stringWithFormat:@"%i",cell.identifierTag]];
        NSLog(@"Removed: %i",cell.identifierTag);
      //      NSLog(@"setting star image: 2");
        [star setImage:[UIImage imageNamed:@"star_empty.png"]];
        [star setTag:kStarEmpty];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *teacher = [[Global sharedGlobal].teachers objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    UIImageView *starView = [[UIImageView alloc] init];
    starView.image = [UIImage imageNamed:@"star_empty.png"];
    starView.frame = CGRectMake(720, 2, 29, 29); //748,22
    [starView setTag:kStarEmpty];
    cell.star = starView;
    [cell addSubview:starView];

    cell.identifierTag = indexPath.row;

    NSLog(@"This cell's identifier tag: %i",cell.identifierTag);

    cell.textLabel.text = [[Global sharedGlobal].teachers objectAtIndex:indexPath.row];

    for (int i = 0; i < [[Global sharedGlobal].favTeachers count]; i++) {
        int favTeacherTag = [[[Global sharedGlobal].favTeachers objectAtIndex:i] intValue];
        NSLog(@"%i",i);
        NSLog(@"Fav Teacher Tag: %i",favTeacherTag);
        if (cell.identifierTag == favTeacherTag) {
            NSLog(@"found fav teacher: %i",cell.identifierTag);
            NSLog(@"------------------------------------------");
    //        NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[cell.contentView subviews]];

            NSLog(@"setting star image");
            [starView setImage:[UIImage imageNamed:@"star.png"]];
            NSLog(@"Previous star tag: %i",starView.tag);
            [starView setTag:kStarFilled];
            break;
        }

        NSLog(@"--------------------------------");
    }

    return cell;
}

额外信息:

  • 我有一个自定义类的单元格,它将cell.identifierTag添加为int

  • 我正在使用Storyboard

  • 我在故事板中使用静态单元格

谢谢!如果您需要更多信息,请发表评论并询问。

3 个答案:

答案 0 :(得分:2)

您需要确保在 else 块中将UIImage设置为nil

if (cell.identifierTag == favTeacherTag) {
    //your existing code
} else {
    [starView setImage:nil];
};

这是因为细胞被重复使用,您可能会获得之前已添加星形图像的细胞。

答案 1 :(得分:1)

在(void)tableView结束时调用[tableView reloadData] :( UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法

答案 2 :(得分:0)

经过两天的努力,我终于明白了。 :d

我添加了这段代码,因为我有时会使用相同的UIImageView,并且每次调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath时都会不断添加图片视图。

UIImageView *starView = [[UIImageView alloc] init];

if (cell.star == nil) {
    starView.image = [UIImage imageNamed:@"star_empty.png"];
    starView.frame = CGRectMake(720, 2, 29, 29); //748,22
    [starView setTag:kStarEmpty];
    cell.star = starView;

    [cell addSubview:starView];
} else if (cell.star != nil) {
    starView = cell.star;
}
相关问题