uitableviewcell中的checkBox动作事件

时间:2015-10-06 07:36:33

标签: ios

我有自定义单元格,其中我将UIButton作为CheckBox 我想在ViewController中选择checkBox indexPath的数组 我试图将UIButton Action事件设置为ViewController,但它无法访问。
这是我的代码,

-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCellSaveSearchTableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:@"SaveSearchCell"];
    if(!cell)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCellSaveSearchTableViewCell" owner:self options:nil]objectAtIndex:0];
    }
    [self setText:cell];
    [cell.btnDelete addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.btnDelete.tag = indexPath.row;
    return cell;
}
-(void)btnClick : (UIButton *)btn
{
    // I Can't accaes this method.
}


我怎么能得到这个。

3 个答案:

答案 0 :(得分:0)

UITableViewCells具有自己的状态,以反映它们是否被选中。您不需要单元格中的按钮(因为UX原因,不鼓励它)。查看here获取教程。

答案 1 :(得分:0)

Do according to these three floowing method.

- (void)viewDidLoad {
[super viewDidLoad];
chekMarkArray = [[NSMutableArray alloc]init];
int totalData = 10;
chekMarkArray=[[NSMutableArray alloc] initWithCapacity:totalData];
for (int i=0; i<totalData; i++) {
     [chekMarkArray addObject:@(0)];
}
}


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

UIButton *testButton;
if (cell == nil ||1) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testButton.frame = CGRectMake(100, 5,25, 25);
    testButton.tag = indexPath.row;
    BOOL selectindex = [[chekMarkArray objectAtIndex:indexPath.row] boolValue];
    if (!selectindex) {
        testButton.backgroundColor = [UIColor greenColor];
    }else{
        testButton.backgroundColor = [UIColor redColor];
    }

    [testButton addTarget:self action:@selector(DropDownPressed:) forControlEvents:UIControlEventTouchDown];
    [cell addSubview:testButton];
}

return cell;
}

checked button action

-(void)btnClick : (UIButton *)btn
{
UIButton *button = (UIButton*)sender;
BOOL currentStatus = [[chekMarkArray objectAtIndex:button.tag] boolValue];
//deselect all selection
for (int i=0;i<chekMarkArray.count;i++) {
    [chekMarkArray replaceObjectAtIndex:i withObject:@(0)];//comment this line for multiple selected button
}
//select current button
[chekMarkArray replaceObjectAtIndex:button.tag withObject:@(!currentStatus)];
[self.tableView reloadData];
}

答案 2 :(得分:0)

您可以使用此代码代替按钮

ViewDidLoad中的第一个Alloc NsmutableArray 并在Tableview外部放置一个Button来完成您的操作。

然后添加

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
   [selectedRowsArray addObject:[arrShareTickets objectAtIndex:indexPath.row]];
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
    [selectedRowsArray removeObject:indexPath];
}
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}