在我的 iPhone UITable视图应用程序
中当用户在桌面视图单元格/行上滑动时,我需要添加两个按钮。
一个撰写邮件按钮一个发送短信按钮给联系人。
我实现了以下代码,仅用于添加发送邮件(蓝云按钮)
//Add a left swipe gesture recognizer in view Did load
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeRow:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[contactsTable addGestureRecognizer:recognizer];
[recognizer release];
- (void)swipeRow:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:contactsTable];
NSIndexPath *indexPath = [contactsTable indexPathForRowAtPoint:location];
UITableViewCell *cell = [contactsTable cellForRowAtIndexPath:indexPath];
UIButton *MailButton = [UIButton buttonWithType:UIButtonTypeCustom];
[MailButton setBackgroundImage:[UIImage imageNamed:@"sendData.png"] forState:UIControlStateNormal];
MailButton.frame=CGRectMake(150, 0, 40.0, 30.0);
[MailButton addTarget:self action:@selector(SendMail) forControlEvents:UIControlEventTouchUpInside];
//Add mail button if index path is valid
if(indexPath)
{
[cell addSubview:MailButton];
}
}
当我们在单元格上滑动时,它会执行正常并向所选单元格/行添加按钮。
But when we swipe another cell the buttons on the previous cell are does not hide / removed
重复如下
我想在其他单元格上显示(删除)按钮,同时在另一个单元格上轻扫一下只有一个云按钮一次..
答案 0 :(得分:1)
为什么不将按钮保留在XIb中并将其添加到相应的单元格?它是整个类的单个对象,因此以前的单元格中没有按钮的重复。请试试。
答案 1 :(得分:0)
您需要的是一个用于存储您添加云按钮的lastIndexPath的ivar。 每次添加新按钮时,您必须从以前添加的indexPath中删除该按钮,并将新的indexPath分配给ivar。
您的代码可能看起来像这样(未经过测试)。
在属性上的.h文件中
@property (strong, nonatomic) NSIndexPath *lastIndexPath;
并在.m文件中合成它。
现在代码看起来像这样
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeRow:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[contactsTable addGestureRecognizer:recognizer];
[recognizer release];
- (void)swipeRow:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:contactsTable];
NSIndexPath *indexPath = [contactsTable indexPathForRowAtPoint:location];
//New Code
UITableViewCell *cell = [contactsTable cellForRowAtIndexPath:indexPath];
if(lastIndexPath != nil)
UITableViewCell *last = [contactsTable cellForRowAtIndexPath:lastIndexPath];
//End
UIButton *MailButton = [UIButton buttonWithType:UIButtonTypeCustom];
[MailButton setBackgroundImage:[UIImage imageNamed:@"sendData.png"] forState:UIControlStateNormal];
MailButton.frame=CGRectMake(150, 0, 40.0, 30.0);
[MailButton addTarget:self action:@selector(SendMail) forControlEvents:UIControlEventTouchUpInside];
MailButton.tag = 1111;// New code
//Add mail button if index path is valid
if(indexPath)
{
//New Code
UIButton *mb = (UIButton *) [cell viewWithTag:1111];
[mb removeFromSuperview];
//End
[cell addSubview:MailButton];
lastIndexPath = indexPath;// New Code
}
}