0

私はUITableViewControllerを作成して人の名前をリストし、その名前の横に星を付けて、好きな人をそのように示しています

多くのセルの 1 つ

タッチすると星が点灯し、お気に入りであることを示します。そのセルの行番号は、このメソッドで呼び出される NSMutableArray に入ります。

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

セルをタップしてインデックスを配列に追加すると、下にスクロールして星がいっぱいになるまで、すべてが機能します。それらはランダムにあると思いますが、上下にスクロールするたびにいくつかのポップアップが表示され、このように色あせた星が表示されます...

色あせた星

これは満点の星

フルスター

なぜか埋められないはずの星が薄れている。

星がオンになっているポイントを特定できません。ログには、特定のセルにスクロールしたときに星をオンに設定することのみが表示されます。

私の問題は、星がオンになってはならないときにオンになることです。私の配列は良好です。何度も確認しましたが、UITableView でなければなりません。

これは私のコードです、

その星の画像は 2 つしかありません。1 つは塗りつぶされ、もう 1 つは空です。

- (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

  • ストーリーボードを使用しています

  • ストーリーボードで静的セルを使用しています

ありがとうございました!さらに情報が必要な場合は、コメントして質問してください。

4

3 に答える 3

2

ここで、elseブロックに必ず to を設定する必要がありUIImageます。nil

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

これは、セルが再利用されるためであり、以前にスター イメージが追加されたセルを取得している可能性があります。

于 2013-11-13T21:04:46.750 に答える
1

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath メソッドの最後で [tableView reloadData] を呼び出します

于 2013-11-13T21:14:37.473 に答える