细胞按钮的动作

时间:2014-01-04 05:29:47

标签: ios uitableview

我正在使用tableview,其中我在一个单元格上添加了2个按钮。以下是我使用的代码

- (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];
    }

    cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tickbtn.tag = 200+indexPath.row;
    [tickbtn setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
    [tickbtn addTarget:self action:@selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
    tickbtn.frame = CGRectMake(220, 10, 30, 30);
    [cell.contentView addSubview:tickbtn];
    NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);

    crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    crossbtn.tag = 400+indexPath.row;
    [crossbtn setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
    [crossbtn addTarget:self action:@selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
    crossbtn.frame = CGRectMake(250, 10, 30, 30);
    [cell.contentView addSubview:crossbtn];
    NSLog(@"tickbtn tag %ld",(long)crossbtn.tag);

    return cell;
}
在tickbtn和crossbtn上的

我正在应用以下行动: -

-(IBAction)addshed:(UIControl *)sender
{
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag-200 inSection:0];
    UITableViewCell *cell = (UITableViewCell*)[list_table cellForRowAtIndexPath:indexPath];
    UIButton *check1 = (UIButton*)[cell.contentView viewWithTag:indexPath.row+200];
    UIButton *check2 = (UIButton*)[cell.contentView viewWithTag:indexPath.row+400];
    UIImageView *btnimg1 = [[UIImageView alloc] initWithImage:check1.currentBackgroundImage];
    //UIImageView *btnimg2 = [[UIImageView alloc] initWithImage:check2.currentBackgroundImage];
    NSLog(@"SHED LIST subviews: %@", btnimg1.image);
    // Shed_data *sheddata  = [[Shed_data alloc] init];
    if (btnimg1.image == [UIImage imageNamed:@"ok_gray.png"]) {
        //btnimg.image = [UIImage imageNamed:@"ok_gray.png"];
        [check1 setBackgroundImage:[UIImage imageNamed:@"ok_green.png"]forState:UIControlStateNormal];
        [check2 setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
        [self addsheddata:sender];
        NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);
    }
     else if (btnimg1.image == [UIImage imageNamed:@"ok_green.png"])
     {
        [check2 setBackgroundImage:[UIImage imageNamed:@"delete-red.png"]forState:UIControlStateNormal];
        [check1 setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
        [self removesheddata:sender];

}


}
-(IBAction)removeshed:(UIControl*)sender
{
  //.…………………….. My functionality
}

但是在这两种情况下,只有当我按下单元格的按钮时,才会获得最后一个单元格的标记值。 请找出我的错误并帮助我解决它。你的帮助会非常明显。

4 个答案:

答案 0 :(得分:1)

试试这个对我来说很好。我刚用我的Xcode 5进行了测试。

修改: 1.我创建一个名为_objects NSMutableArray的{​​{1}}。并将其提交给我的_objects = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"thre", nil];

2.给tickBtn和crossBtn一个不同的颜色,这样很容易看到。

3.将按钮按下功能更改为UITableViewUIControl,如UIButton,当按下按钮时,我会抓住标签值并将其打印在控制台上。

-(IBAction)addshed:(UIButton *)sender

新问题更新

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

    cell.textLabel.text = [_objects objectAtIndex:indexPath.row];

    cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tickbtn.tag = 200+indexPath.row;
    [tickbtn setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
    [tickbtn setBackgroundColor:[UIColor blackColor]];
    [tickbtn addTarget:self action:@selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
    tickbtn.frame = CGRectMake(220, 10, 30, 30);
    [cell.contentView addSubview:tickbtn];
    NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);

    crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    crossbtn.tag = 400+indexPath.row;
    [crossbtn setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
    [crossbtn addTarget:self action:@selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
    crossbtn.frame = CGRectMake(250, 10, 30, 30);
    [crossbtn setBackgroundColor:[UIColor greenColor]];

    [cell.contentView addSubview:crossbtn];
    NSLog(@"tickbtn tag %ld",(long)crossbtn.tag);

    return cell;
}

-(IBAction)addshed:(UIButton *)sender   {
    NSLog(@"add shed %d",sender.tag);
}

-(IBAction)removeshed:(UIButton *)sender   {
    NSLog(@"remove %d",sender.tag);
}

结果是

enter image description here enter image description here

正如另一个答案所说

Did you try with 10 or more cells and try with some continuous scroll?

让我清楚这一点,因为我知道它。

contentView是[cell addSubview:crossbtn];// -------- Change here --------- 的子视图。 please review this reference在这里您可以看到UITableViewCell中实际上有3个子视图。

答案 1 :(得分:0)

我见过问题。这可能会导致此类错误。

Why do you add subviews again and again to your cell's content view.?

也就是说,对于每个cellForRowAtIndexpath:调用,按钮将添加到单元格。在dequeueReusableCellWithIdentifier:的情况下,您的最后一个单元格可以在滚动时重复使用任何其他单元格。这会导致你的错误。那就是你的单元格将包含两个按钮。(带有最后一个单元格的标签和当前单元格的标签)。

在这一行[cell.contentView addSubview:tickbtn];中,你必须根据add一次和crossbtn进行一些更改。

更新:我看到了您更新的问题。我的建议,更好地使用自定义单元格。使用此链接how to create custom cell.。你的代码中有很多混乱。恩。在这一行UITableViewCell *cell = (UITableViewCell*)[list_table cellForRowAtIndexPath:indexPath];中,它会给出意想不到的结果。不要像这样调用委托方法。

答案 2 :(得分:0)

您需要将按钮添加到单元格子视图而不是单元格的contentview子视图。所以使用这段代码......

- (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];
        }

        cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tickbtn.tag = 200+indexPath.row;
        [tickbtn setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
        [tickbtn addTarget:self action:@selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
        tickbtn.frame = CGRectMake(220, 10, 30, 30);
        [cell addSubview:tickbtn];// -------- Change here ---------
        NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);

        crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        crossbtn.tag = 400+indexPath.row;
        [crossbtn setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
        [crossbtn addTarget:self action:@selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
        crossbtn.frame = CGRectMake(250, 10, 30, 30);
        [cell addSubview:crossbtn];// -------- Change here ---------
        NSLog(@"tickbtn tag %ld",(long)crossbtn.tag);

        return cell;
    }

答案 3 :(得分:0)

您的代码似乎适用于cellForRowAtIndexpath:。左错误可能是在按钮点击时获取标记值。尝试使用此代码进行更改: -

-(IBAction)addshed:(UIControl *)sender
{
    //.…………………….. My functionality

    int selectedRow1 = ((UIControl *)sender).tag;
    NSLog(@"No. %d", selectedRow1);

}
相关问题