Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 单元格按钮的操作_Ios_Uitableview - Fatal编程技术网

Ios 单元格按钮的操作

Ios 单元格按钮的操作,ios,uitableview,Ios,Uitableview,我正在使用tableview,其中我在一个单元格上添加了两个按钮。下面是我使用的代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableC

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

- (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和crosstn上,我正在应用以下操作:-

-(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
}
但在这两种情况下,我只有在按下单元格的按钮时才能得到最后一个单元格的标签值。
请找出我的错误并帮助我解决它。你们的帮助将非常可观。

我已经看到了这个问题。这可能会导致这种类型的错误

Why do you add subviews again and again to your cell's content view.?
也就是说,对于每个
cellForRowAtIndexpath:
调用,按钮都将添加到单元格中。如果
dequeueReusableCellWithIdentifier:
,您的最后一个单元格可以在滚动时重用到任何其他单元格。这会导致你的错误。这就是您的单元格将包含两个按钮。(用最后一个单元格标记,用当前单元格标记)

在这一行
[cell.contentView addSubview:tickbtn],您必须根据add进行一些更改一次,也可以对CROSBTN进行更改


更新:我看到了您更新的问题。我的建议是,最好使用自定义单元格。使用此链接可以访问。代码中有很多混乱。例如,在这一行中,
UITableViewCell*单元格=(UITableViewCell*)[list_table cellforrowatinexpath:indexath],它将给出意外的结果。不要像这样调用委托方法。

试试这个方法,因为它对我来说很好用。我刚刚用我的
Xcode 5
进行了测试

修改: 1.我创建了一个名为_objects(
_objects=[[NSMutableArray alloc]initWithObjects:@“一”、“二”、“三、零”)的
NSMutableArray
)。并将其交给我的
UITableView

2.给tickBtn和CrossTn一个不同的颜色,以便容易看到

3.将按钮按下功能更改为
UIControl
UIButton
-(iAction)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);
}
新问题更新

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

正如另一个答案所说

[cell addSubview:crossbtn];// -------- Change here ---------
据我所知,让我澄清一下


contentView是
UITableViewCell
的子视图。在这里,您可以看到在
UITableViewCell
中实际上有3个子视图。您需要将按钮添加到单元格子视图,而不是单元格的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;
    }

对于
cellForRowAtIndexpath:

而言,您的代码似乎很好。单击按钮时获取标记值时可能会出错。尝试使用此代码进行更改:-

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

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

}

您能否显示-(iAction)添加:(UIControl*)发件人的代码??您是如何获得标签值的?请描述更多在您的按钮操作触发并完成后将发生的情况?当我按下特定单元格的按钮时,它将更改单元格的颜色…@user3131561:使用customCell可以轻松完成此操作。查看我的更新答案。什么是
列表\u表
?从何处获取
UITableViewCell
对象以及在何处使用它?是否确定在这一行
[cell.contentView addSubview:tickbtn],按钮将只向单元格添加一次?如果滚动或重复使用单元格?@Mani,根据要求,它可以正常工作。请查看更新的答案,年轻人,并感谢您的评论/讨论。幸运的是,你尝试了10个或更多的单元格,尝试了一些连续滚动,并且只给了cell.contentview.subviews.count日志?让我们@bunty……………。我在addshed函数中做了更新,请看一看,并告诉我是否有错误的地方…。