0

ストーリーボードを介してtableViewを設計しました。1つのセルに1つのボタンと1つのラベルがあります。ボタンにはタグ1があり、ラベルにはストーリーボードにタグ2があります。cellForRowAtIndexPathで私は以下のようにこれらにアクセスしています

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@" i am back in cellForRowAtIndexPath");  

    static NSString *CellIdentifier = nil;
   // UILabel *topicLabel;
    NSInteger rows=indexPath.row;
    NSLog(@"row num=%i",rows);
    if (rows % 2==0)
    {
        CellIdentifier=@"LeftTopicCell";
    }
    else
    {
        CellIdentifier=@"RightTopicCell";

    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UIButton *topicButton=(UIButton *)[cell viewWithTag:1];
    UILabel  *topicScoreLabel=(UILabel *)[cell viewWithTag:2];

    NSString *topicNameLabel=[[NSString alloc]init];

    //some logic processing


            topicNameLabel= topicItem.topicTitle;

            [topicButton setTitle:topicNameLabel forState:UIControlStateNormal];
        [topicButton setTag:indexPath.row ];
            [topicButton setTitle:topicNameLabel forState:UIControlStateHighlighted];
            [topicButton addTarget:self action:@selector(topicButtonSelected:) forControlEvents:UIControlEventTouchDown];

        [topicScoreLabel setText:[NSString stringWithFormat:@"%d/%d",correctAnswers,[answerResults count]]];
    }

    return cell;
}

初めては正常に動作しますが、このビューコントローラに戻ったときに、これを再度リロードしましたが、今回は2行で正常に動作します。ただし、3行目では、この行のUILabelではなくUIButtonが返されます。このUILabel *topicScoreLabel=(UILabel *)[cell viewWithTag:2];ため、「認識されないセレクター"[UIButtonsetText]"」という例外が発生します。

4

2 に答える 2

4

ボタンとラベルをタグ1と2として取得し、次にタグ値をindexpath.rowに設定しているため、問題が発生しています。

 [topicButton setTag:indexPath.row ];

したがって、他のタグ値をボタンに設定し、1000のようなラベルを付けます。

次に、アクセスします

UIButton *topicButton=(UIButton *)[cell viewWithTag:1000];
UILabel  *topicScoreLabel=(UILabel *)[cell viewWithTag:1001];
于 2013-01-30T10:17:47.693 に答える
0

行番号に従ってcellForRowAtIndexPathのsetTagメソッドを介してボタンタグを更新しているため、回答が得られました。タグをボタンに番号2として設定しているため、UIButtonが返されます。

于 2013-01-30T10:11:30.510 に答える