3

我在 UITableView 中以编程方式创建了两个按钮
即编辑删除

当我们单击单元格时,会显示这些按钮,但是当我尝试单击编辑或删除按钮时,它不会调用适当的方法,即编辑或 deleteBtn。
这是我的代码。

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

    NSString *cellValue=[firstName objectAtIndex:indexPath.row];
    cell.textLabel.text=cellValue;


    edit=[[UIButton alloc]init];
    [edit setTitle:@"Edit" forState:UIControlStateNormal];
    [edit setFrame:CGRectMake(100, 100, 100, 20)];
    [edit setTag:1];
    [edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

    delete=[[UIButton alloc]init];
    [delete setTitle:@"Delete" forState:UIControlStateNormal];
    [delete setFrame:CGRectMake(150, 100, 100, 20)];
    [delete setTag:2];
    [delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:delete];
    [cell.contentView addSubview:edit];

    return cell;

}

我的编辑和删除功能很简单

-(void)edit{

NSLog("%@",selectedValue);
}

-(void)deleteBtn{

NSLog("%@",selectedValue);
}

我已经检查了这个应用断点的函数,但它没有被调用。

这就是我的selectedValue来的方式

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedValue=[firstName objectAtInder:indexPath.row];
}

谢谢,提前,
阿伦。

4

10 回答 10

2

想想,问题出在UIButton框架上。在您的代码中,两个按钮origin.y都是 100.0,因此它超出了单元格的边界(默认高度为 44.0)。

像这样改变你的两个UIButton框架,它对我有用。

UIButton *  edit =[[UIButton alloc]init];
[edit setBackgroundColor:[UIColor blackColor]];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
**[edit setFrame:CGRectMake(100,5.0,100, 30)];**
[edit setTag:1];
[edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

UIButton*  delete=[[UIButton alloc]init];
[delete setBackgroundColor:[UIColor blackColor]];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
**[delete setFrame:CGRectMake(200,5.0,100,30)];**
[delete setTag:2];
[delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
于 2013-03-18T06:13:37.063 回答
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]autorelease];
}

NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;


edit=[[UIButton alloc]init];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 5, 100, 20)];
[edit setTag:indexPath.row];
[edit addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:edit];   

delete=[[UIButton alloc]init];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(210, 5, 100, 20)];
[delete setTag:indexPath.row];
[delete addTarget:self action:@selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:delete];

return cell;

}

使用如下方法

-(IBAction)edit:(id)sender
{
    int tag = [sender tag];
    NSString *str = [firstName objectAtIndex:tag];
    NSLog("%@",str);
 }

 -(IBAction)deleteBtn:(id)sender
 {
    int tag = [sender tag];
    NSString *str = [firstName objectAtIndex:tag];
    NSLog("%@",str);
 }
于 2013-03-18T06:04:09.727 回答
1

问题出在setFrame

表格单元格的高度小于您为按钮设置的坐标。将代码更改为

[edit setFrame:CGRectMake(100, 5, 100, 20)]; 

这将起作用。

或者

只需增加height表格行的数量,默认情况下是44,使用

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
于 2013-03-18T06:20:28.843 回答
1

您的按钮被一遍又一遍地添加到单元格中,因为每次调用 CFRAIP 时都会创建它们。将它们移动到cell == nil块中:

-(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];
        edit=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
        [edit setTitle:@"Edit" forState:UIControlStateNormal];
        [edit setFrame:CGRectMake(100, 100, 100, 20)];
        [edit setTag:1];
        [edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

        delete=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
        [delete setTitle:@"Delete" forState:UIControlStateNormal];
        [delete setFrame:CGRectMake(150, 100, 100, 20)];
        [delete setTag:2];
        [delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:delete];
        [cell.contentView addSubview:edit];
    }

    NSString *cellValue=[firstName objectAtIndex:indexPath.row];
    cell.textLabel.text=cellValue;
    return cell;

}
于 2013-03-18T06:02:06.247 回答
0

尝试这个::

表法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Configure the cell.
   UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

      NSString *cellValue=[firstName objectAtIndex:indexPath.row];
      cell.textLabel.text=cellValue;

      UIButton *btnEdit=[UIButton buttonWithType:UIButtonTypeCustom];
      [btnEdit setTitle:@"Edit" forState:UIControlStateNormal];
      [btnEdit setFrame:CGRectMake(100, 100, 100, 20)];
      [btnEdit setTag:indexPath.row];
      [btnEdit addTarget:self action:@selector(clickEdit:) forControlEvents:UIControlEventTouchUpInside];

      [cell.contentView addSubview:btnEdit];


      UIButton *btnDelete=[UIButton buttonWithType:UIButtonTypeCustom];
      [btnDelete setTitle:@"Delete" forState:UIControlStateNormal];
      [btnDelete setFrame:CGRectMake(150, 100, 100, 20)];
      [btnDelete setTag:indexPath.row];
      [btnDelete addTarget:self action:@selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];

      [cell.contentView addSubview:btnDelete];
   }
   return cell;
}

然后,按钮操作方法

-(IBAction)clickEdit:(id)sender
{
   NSString *str = [firstName objectAtIndex:[sender tag]];
   NSLog("Name :: %@",str);
}

-(IBAction)clickDelete:(id)sender
{
   NSString *str = [firstName objectAtIndex:[sender tag]];
   NSLog("Name :: %@",str);
}

希望它会帮助你。

谢谢。

于 2013-03-18T06:23:46.833 回答
0

像这样修改您的代码....

替换以下两行...

edit=[[UIButton alloc] init];
delete=[[UIButton alloc] init];

像这样……

UIButton *edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];

UIButton *delete=[UIButton buttonWithType:UIButtonTypeRoundedRect]];
于 2013-03-18T06:08:39.670 回答
0

不要使用[UIButton alloc] init]. 改用这个

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(edit:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Edit" forState:UIControlStateNormal];
 button.frame = CGRectMake(80.0, 21.0, 40.0, 40.0);
[view addSubview:button];

其中 view 是将按钮添加为子视图的视图。您也可以根据您的要求设置按钮框架。

于 2013-03-18T06:10:39.827 回答
0

请尝试使用这个......并为删除按钮做同样的事情............

if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        // -------------------- Add button to cell --------------
        edit = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 20)];

        [edit setTitle:@"Edit" forState:UIControlStateNormal];
        [edit setTag:1];
        [edit addTarget:self action:@selector(edit)   forControlEvents:UIControlEventTouchUpInside];        

        [cell.contentView addSubview:edit];
    }
于 2013-03-18T06:03:13.603 回答
0

请更改按钮名称,因为 Edit 和 delete 已经在 Objective C 中定义了关键字

于 2013-03-18T06:06:40.773 回答
0

我可以尝试自定义单元格,您可以在自定义单元格视图中添加按钮并实现以下代码。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    cell = [self.tbl dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {


        //****cell recent deal buy button click goes to login or check out pageview 
        //[cell.btn setTag:indexPath.row];
        //[cell.btn addTarget:self action:@selector(buybutton_checkout:)
       //   forControlEvents:UIControlEventTouchDown];
       // cell.btn.hidden=NO;


        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"icel" owner:nil options:nil];

        for (UIView *view in views)
        {
            if([view isKindOfClass:[UITableViewCell class]])
            {

                cell = (icel*)view;

            }

        }
    }

   //tbl.layer.cornerRadius = 3.9;
    //[tbl setClipsToBounds:YES];


    [cell.activity startAnimating]; 

    [cell.btn setTitle:_BUYNOW_BTN forState:UIControlStateNormal];
    [cell.btn setTag:indexPath.row];
    [cell.btn addTarget:self action:@selector(Buy_btnlck:)
                forControlEvents:UIControlEventTouchDown];
        //cell.btn.hidden=NO;
    cell.title_lbl.text=[[_today_similardeal_title_ary objectAtIndex:indexPath.row]capitalizedString];


        index_tbl=indexPath.row;
    return cell;
    }
}
-(IBAction)Buy_btnlck:(UIButton *)button
{
NSInteger intvalue=[[NSString stringWithFormat:@"%ld",(long int)[button tag]]intValue];
}
于 2013-03-18T06:07:31.847 回答