Objective c uibutton将其设置为在uitableview的uitableview单元格中可见
我在UITableView的UITableView单元格中有一个UIButton。按钮是隐藏的。当用户用手指在特定UITableViewCell上向左滑动时,将显示该按钮 我使用这段代码来实现它,它正在工作,但是按钮显示在多个uitableviewcells中,而不是用户用手指滑动的那个Objective c uibutton将其设置为在uitableview的uitableview单元格中可见,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我在UITableView的UITableView单元格中有一个UIButton。按钮是隐藏的。当用户用手指在特定UITableViewCell上向左滑动时,将显示该按钮 我使用这段代码来实现它,它正在工作,但是按钮显示在多个uitableviewcells中,而不是用户用手指滑动的那个 - (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestu
- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
UIView *tappedview=[gestureRecognizer.view hitTest:[gestureRecognizer locationInView:gestureRecognizer.view] withEvent:nil];
UIView *contentview1=tappedview.superview;
UIView *viewwithtag4=[contentview1 viewWithTag:7009];
UIButton *button2=(UIButton *)viewwithtag4;
NSLog(@"swipe left detected");
[button2 setHidden:FALSE];
}
}
感谢您的帮助!谢谢。如果滚动后按钮显示在错误的单元格中,那是因为tableView正在重用TableCell以提高性能。i、 e 如果要使特定单元格的按钮保持可见,则必须执行以下操作: 在由手势识别器调用的方法中,保存按钮的状态。您必须确定已刷过的单元格,然后将该状态保存在您正在填充单元格的类/模型中。i、 将您的数据源重新命名。例如,如果数据源是数组中的一个对象,则可以按以下方式执行操作
// in your cellSwiped method and assuming you can traverse the view hierarchy to get
// the tableViewCell.
NSIndexPath *theCellIndexPath=[self.tableView indexPathForCell: theTableViewCell];
MyDataSourceObject *theDataSourceObject=[dataObjectArray objectAtIndex: theCellIndexPath.row];
// The buttonIsVisible ivar for your data source could be name that
// or something else that is meaningful. Not sure what the button i
// related to in you objects
theDataSourceObject.buttonIsVisible=YES // or you could put in code to toggle the value
然后在cellForRowAtIndexPath方法中,必须将按钮设置为隐藏或不隐藏,这取决于特定indexPath的状态
MyDataSourceObject *theDataSourceObject=[dataObjectArray objectAtIndex: indexPath.row];
cell.button.hidden=theDataSourceObject.buttonIsVisible;
return cell;
祝你好运我将单元格的按钮保存在dataObjectArray中,然后从dataObjectArray调用它,但同样的情况也发生了@斯特凡诺森。首先,您不需要保存按钮。您只需要确保您正在记录与一段数据有关的按钮状态。在cellForRowAtIndexPath方法中,确保正在设置按钮是否隐藏在if语句之外,该语句检查已重用的单元格是否为nil。请发布cellForRowAtIndexPath方法的代码哦,天哪,你是对的timthetoolman!button.hidden应该在if(cell==nil)之外!现在它是工作,但当我向下滚动按钮消失,但我认为这是它应该如何工作!也许我能在卷轴开始时找到一个隐藏它的方法!非常感谢!:)我已经搜索了更多关于这个问题的信息,下面的一些代码可能会帮助一些人label2.backgroundColor=[UIColor colorWithPatternImage:image];[标签2设置需要显示];使用cgrectmake添加图像时,必须调用setneeddisplay。您必须在cellforrowatindexpath中使用以下代码NSString*CellIdentifier=[NSString stringWithFormat:@“Cell%i-%i”,indexath.section,indexPath.row];我花了5个小时在这上面,希望它能帮助别人!要有信心!