我想在长按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
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
[cellView addSubview: button];
在创建完成后再:
[cellView addSubview: button]; 才可以
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;我应该把这段代码加上的,谢谢了。