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

标签 objective-c ios uitableview

我在 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/

相关文章:

ios - CoreData 扩展 - 将模型添加到目标时为 'Use of Undeclared Identifier'

ios - 添加到 UIButton 的 UIButton 不响应选择器

ios - 如何在 iPhone 上控制硬件麦克风输入增益/电平?

objective-c - GL_TRIANGLE_STRIP 和顶点数组出错

objective-c - 为什么 addSubview 不显示?

ios - 如何将图像从 uiimagepickercontroller 传递到另一个场景的 uiimageview

ios - Apple 在核心数据的上下文中将什么称为对象图?

objective-c - 如何使用 objective c 在 iphone 的表格 View 上应用复选标记?

ios - 处理单个单元格选择及其在 UITableView 中的操作

ios - UITableView Header Override (UIView) 滚动行为异常