【发布时间】:2012-08-01 07:00:24
【问题描述】:
我正在显示自定义单元格中的表格视图,并且工作正常。 但问题是在 tableCell 内创建的 UiButton 没有响应选择器。
我滑动行进行编辑然后我在 titleForDeleteConfirmationButtonForRowAtIndexPath 中创建 UIButton。 这出现在删除按钮的同时,触摸删除按钮响应 commitEditingStyle: 然后删除按钮和添加按钮 Disappers。我再次滑动行然后出现添加和删除按钮如果我点击添加按钮它总是响应 canEditRowAtIndexPath 然后 didEndEditingRowAtIndexPath。
我不明白为什么它不调用 btnMethod。请告诉我如何使 btnMethod 可以从 uibutton touchDown 事件中调用。
这是我的代码:
- (void)tableView:(UITableView *)tableView1 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];
UIButton *btn=(UIButton*)[cell11.contentView viewWithTag:indexPath.row+433];
btn.hidden=YES;
[btn removeFromSuperview];
}
}
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (NSString *)tableView:(UITableView *)tableView1 titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];
UIButton *btnAdd=[UIButton buttonWithType:UIButtonTypeCustom];
btnAdd.frame=CGRectMake(8, 6, 80, 30);
[btnAdd setTitle:@"Add" forState:UIControlStateNormal];
btnAdd.backgroundColor=[UIColor magentaColor];
btnAdd.titleLabel.textAlignment=UITextAlignmentCenter;
btnAdd.titleLabel.textColor=[UIColor whiteColor];
btnAdd.layer.masksToBounds=YES;
btnAdd.layer.cornerRadius=6.0;
btnAdd.layer.borderWidth=2.0;
btnAdd.layer.borderColor=[UIColor blackColor].CGColor;
btnAdd.tag=indexPath.row+433;
[btnAdd addTarget:self action:@selector(btnMethod:) forControlEvents:UIControlEventTouchUpInside];
btnAdd.userInteractionEnabled=YES;
[cell11.contentView addSubview:btnAdd];
return @"Drop Me";
}
- (BOOL)tableView:(UITableView *)tableView1 canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)tableView:(UITableView*)tableView1 didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell11=[tableView1 cellForRowAtIndexPath:indexPath];
UIButton *btn=(UIButton*)[cell11.contentView viewWithTag:indexPath.row+433];
btn.hidden=YES;
[btn removeFromSuperview];
}
-(void)btnMethod:(id)sender
{
UITableViewCell *cell=(UITableViewCell*)[sender superview];
}
【问题讨论】:
-
你的这个方法永远不会被调用,这就是为什么永远不会添加按钮。 - (NSString *)tableView:(UITableView *)tableView1 titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 最好在 cellForRowAtIndexPath 中添加按钮
-
添加了亲爱的按钮,当单元格滑动问题是它从不接受触摸而删除按钮执行时,它与删除按钮一起出现
-
好的,让我检查一下..我正在实施所有这些。
-
我试过了..但我想在删除模式下只有删除按钮可以工作..所以这是不可能的..
-
@iPhoneDeveloper 非常感谢您的努力
标签: iphone uitableview