读书人

如何在其他方法里给UITableView cell下

发布时间: 2013-01-20 10:22:40 作者: rapoo

怎么在其他方法里给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];


[cellView addSubview: button];


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

读书人网 >Iphone

热点推荐