1

動的な行数で 4 つのセクションを作成UITableViewしました。セルをカスタマイズし、ボタンを非常にセルに追加しました。ボタンは別のセクションで再利用されています。

私のコード:

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



 {



     static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil)


     {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

      }


     self.buttonMe = [UIButton buttonWithType:UIButtonTypeCustom];
    [ self.buttonMe setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
    self.buttonMe.tag=i;

    if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])

    [ self.buttonMe setImage:[UIImage imageNamed:@"check.gif"] forState:UIControlStateNormal];

    else

    [ self.buttonMe setImage:[UIImage imageNamed:@"uncheck.jpg"] forState:UIControlStateNormal];

    [ self.buttonMe addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview: self.buttonMe];

    cell.textLabel.text=@"itsFine";
//"i", i created for assigning the unique tag for created button every time
    i++;

    return cell;

}

なぜそれが起こったのか理由を付けて適切な解決策を教えてください

4

2 に答える 2

0

警告: アイデアを提供するためだけに、テストされておらず、コンパイルされていないコードです。

わかりました、カスタム uitableview セルを作成する方法の概要を説明します

@interface MyCustomCell  : UITableViewCell

@property (nonatomic, strong) UIButton * buttonMe

@end

実装には次のものがあります

@implementation MyCustomCell 
@synthesize buttonMe;

- (UIButton *) buttonMe {
     if(!buttonMe){
      buttonMe = [UIButton buttonWithType:UIButtonTypeCustom];
      [buttonMe setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
     }
return buttonMe;
}

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (( self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] )) {
     [self addSubView:self.buttonMe];
      }
 }

@end

次に、次のようにコードを変更できます。

    static NSString *CellIdentifier = @"Cell";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
          }
        [cell.buttonMe setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
        cell.buttonMe.tag=i;

        if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"]){
        [cell.buttonMe setImage:[UIImage imageNamed:@"check.gif"] forState:UIControlStateNormal];
}
        else{
        [cell.buttonMe setImage:[UIImage imageNamed:@"uncheck.jpg"] forState:UIControlStateNormal];
}
        cell.textLabel.text=@"itsFine";
        i++;

        return cell;

最後に、カスタム クラスにボタン クリック ハンドラーを追加します。

于 2013-06-12T11:12:21.730 に答える
-1

あなたが何を尋ねようとしているのか、あなたの質問からは明確ではありません。4つのセクションがあり、それぞれにいくつかの行(セル)があり、すべての行にチェックボタンとチェック解除ボタンがあります。ボタンにタグを付けて、そのボタンのインデックスを取得して何らかのアクションを実行できるようにします。

cellForRowAtIndexPathこれで、テーブルビューを上または下にスクロールするたびに、Rememberが呼び出されます。したがって、テーブルビューをスクロールするたびに「i」がインクリメントされます。

したがって、10行ある場合、すべての行に i = 0 から 9 を設定したと思います。ただし、スクロールするたびに「i」がインクリメントされます。そのため、コード内でタグ値をさらに使用できない場合があります。

テーブルビューを使用することindexPath.rowをお勧めします。これはすべての行で一意になります。

コードを変更して、

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

     if (cell == nil)
     {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        /*Reusing you button*/
        [myButton setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
        [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:myButton];
        cell.textLabel.text = @"itsFine";
      }
      myButton.tag = indexPath.row; // Should not use i at here

    if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
       [myButton setImage:[UIImage imageNamed:@"check.gif"] forState:UIControlStateNormal];
    else
       [myButton setImage:[UIImage imageNamed:@"uncheck.jpg"] forState:UIControlStateNormal];

    return cell;
}
于 2013-06-12T09:56:02.103 に答える