Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.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 UITableViewCells中UIButton的操作_Ios_Uitableview - Fatal编程技术网

Ios UITableViewCells中UIButton的操作

Ios UITableViewCells中UIButton的操作,ios,uitableview,Ios,Uitableview,我在UITableView的每个单元格中实现了一个UIButton,并为此按钮设置了一个操作。现在我想找出这些按钮中的哪一个被点击了: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [

我在UITableView的每个单元格中实现了一个UIButton,并为此按钮设置了一个操作。现在我想找出这些按钮中的哪一个被点击了:

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

    // Configure the cell...
    <...>

    UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    but.frame= CGRectMake(275, 5, 40, cell.frame.size.height-10);
    [but setTitle:@"Map" forState:UIControlStateNormal];
    [but addTarget:self action:@selector(Map:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:but];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;

}

- (IBAction) Map: (id) sender {
    NSIndexPath *indexPath = [[NSIndexPath alloc] init];
    indexPath = [tableView indexPathForCell:(UITableViewCell*)[[sender superview]superview]];
    [delegate callMap: [[[data objectAtIndex:indexPath.section] 
                         objectAtIndex:indexPath.row+1] 
                        objectAtIndex:kNameInArray]

                     : [[[[data objectAtIndex:indexPath.section] 
                          objectAtIndex:indexPath.row+1] 
                         objectAtIndex:kXcoordinateInArray] doubleValue]

                     : [[[[data objectAtIndex:indexPath.section] 
                          objectAtIndex:indexPath.row+1] 
                         objectAtIndex:kYcoordinateInArray] doubleValue]
     ];
}
但它总是向委托发送数据[0][1][*]元素。我的意思是indexPath总是有0个原始部分和0个部分。我的错误在哪里?

请执行以下操作: 在if cell==nil{…}方法中添加按钮,否则在重复使用单元格时,同一单元格中会有许多按钮。 Set cell.contentViow.tag=somevalueidentificationingyourdata一些表示模型的int值。。可能只是对象数组的索引。。在返回牢房之前。
在Map:method中,您可以使用UIButtonsender.superview.tag再次访问该值,并根据该值做出决策

我认为您不需要在此处分配NSIndexPath:NSIndexPath*indexPath=[[NSIndexPath alloc]init];这对我来说很有效,现在我知道按下了什么按钮,但如果按你说的那样,显示按钮可能有点问题:它隐藏在另一个视图下,因此只显示右侧的部分像素。尝试使用帧和自动调整大小遮罩: