0

テーブルのカスタム セルを作成しました。基本的に、私のセルには UIButton があります。その細胞を再利用しています。

ただし、セルが再利用されるとすべての要素も再利用されるため、これらのボタンには問題がありますが、これらのボタンがすべてのセルに固有であることを求めています。

多分誰かがこの機能の解決策を提案できますか?

4

2 に答える 2

0

buttonカスタムセルにプロパティがある場合はどうでしょうか。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath    static NSString *CellIdentifier = @"My Cell Identifier";
        MyCustomCellClass *cell = (MyCustomCellClass *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MyCustomCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        // Configure the cell

        UIButton *myButtonForThisCell = [MyButtonArray objectAtIndex:indexPath.row];

        cell.button = myButtonForThisCell;
        return cell;
    }               
于 2012-10-23T06:25:53.583 に答える
0

次の2つのメソッドを使用してbuttonViewUIVIEWファイルを作成します。

-(NSInteger) getGridIndex
{
    return gridIndex;
}

-(void) setGridIndex:(NSInteger)value
{ 
    gridIndex = value;
}

現在cellforrowatindexpathメソッドにあります

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

        ButtonView *buttonView=[[ButtonView alloc] initWithFrame:CGRectMake(110, 110, 160, 26)];
                buttonView.tag=18;
         UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(55, 0, 60, 26)];
[button addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
         [buttonView addSubview:button];
          [cell addSubview:buttonView];
        }
     ButtonView *buttonView=(ButtonView*)[cell viewWithTag:18];
     NSArray *d=[buttonView subviews];
      UIButton *button=[d objectAtIndex:0];
    //here you can change the setting or title of button or whatever u want to do.
      [buttonView setGridIndex:indexPath.row];
    }

今ボタンアクションで:

-(void)ButtonAction:(UIButton*)sender{
    ButtonView *view = (ButtonView *)[sender superview];
    NSInteger n=[view getGridIndex];
    //here n is the indexpath.row at which the button was tapped..you can write its action accordingly.
}
于 2012-10-23T06:26:00.993 に答える