gpt4 book ai didi

objective-c - uibutton在uitableview的uitableviewcell中设置可见

转载 作者:行者123 更新时间:2023-11-28 17:41:30 25 4
gpt4 key购买 nike

我在 UITableView 的 UITableViewCell 中有一个 UIButton。 UIButton 是隐藏的。当用户用手指在特定的 UITableViewCell 上向左滑动时,按钮就会出现。

我使用这段代码来实现它并且它正在工作,但是按钮显示在多个 uitableviewcells 中,而不是用户滑动手指的 uitableviewcells!

- (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 正在重用 tableCells 以提高性能。即

如果您想让特定单元格的按钮可见,您必须执行以下操作:

在 gestureRecognizer 调用的方法中保存按钮的状态。您将必须确定已被刷过的单元格,然后可能将该状态保存在您填充单元格的类/模型中。即您的数据源。例如,如果您的数据源是数组中的一个对象,您可以按照以下方式做一些事情

// 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;

祝你好运

关于objective-c - uibutton在uitableview的uitableviewcell中设置可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com