我想在长按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

解决方案 »

  1.   

    UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath]; 
    [cellView addSubview: button];
      

  2.   

    在你的代码中,你只是创建了UIButton,但并没有把它添加到cell上去
    在创建完成后再:
    [cellView addSubview: button]; 才可以
      

  3.   

     static NSString *UserListIdentifier = @"UserListIdentifier";
            UITableViewCell *cell = nil;
            cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UserListIdentifier] autorelease];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            }
                cell.selectionStyle=UITableViewCellSelectionStyleNone;我应该把这段代码加上的,谢谢了。