当前位置: 代码迷 >> Iphone >> 如何在其他方法里给UITableView cell下添加按钮
  详细解决方案

如何在其他方法里给UITableView cell下添加按钮

热度:67   发布时间:2016-04-25 06:07:18.0
怎么在其他方法里给UITableView cell上添加按钮?
 我想在长按cell的时候给当前长按的cell添加个修改按钮,求高手帮助!
               UILongPressGestureRecognizer *btnEdit = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];    
               btnEdit.minimumPressDuration = 1.0; //seconds  设置响应时间 
               btnEdit.delegate = self;    
               [self.tableView addGestureRecognizer:btnEdit];  //启用长按事件 
               [btnEdit release]; 

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer

{
    CGPoint point = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];//获取响应的长按的indexpath 
    if (indexPath == nil)
    {
        NSLog(@"没有数据"); 
    }
    else 
    {
        NSLog(@"长按的是: %d", indexPath.row); 
    //添加修改方法
    //cell.selectionStyle=UITableViewCellSelectionStyleNone;
    UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"];
    UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width,
                              buttonUpImage.size.height);
    [button setBackgroundImage:buttonDownImage
                      forState:UIControlStateNormal];
    [button setBackgroundImage:buttonUpImage
                      forState:UIControlStateHighlighted];
    [button setTitle:@"修改" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(updateWebSite:)
     forControlEvents:UIControlEventTouchUpInside];
    //cell.accessoryView = button;
    }
        
}
UITableViewCell

------解决方案--------------------
UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath]; 
[cellView addSubview: button];
------解决方案--------------------
引用:
UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];