【问题标题】:uibutton set it visible in uitableviewcell of uitableviewuibutton 设置它在 uitableview 的 uitableviewcell 中可见
【发布时间】:2011-10-31 17:31:41
【问题描述】:

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

我使用此代码来实现它并且它正在工作,但该按钮显示在多个 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];
    }
}

任何帮助表示赞赏!谢谢。

【问题讨论】:

    标签: objective-c ios uitableview


    【解决方案1】:

    如果按钮在滚动后显示在错误的单元格中,那是因为 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;
    

    祝你好运

    【讨论】:

    • 我将单元格的按钮保存在 dataObjectArray 中,然后我从 dataObjectArray 中调用它,但同样的情况发生了!
    • @stefanosn。首先,您不需要保存按钮。您只需要确保您正在记录与一段数据有关的按钮状态。在您的 cellForRowAtIndexPath 方法中,确保您在 if 语句之外设置按钮是否隐藏,该语句检查已重用的单元格是否为 nil。请发布 cellForRowAtIndexPath 方法的代码
    • 天哪,你是对的 timthetoolman! button.hidden 应该在 if (cell==nil) 之外!现在它正在工作,但是当我向下滚动按钮时,它会消失,但我认为它应该是这样工作的!也许我可以在滚动开始时找到隐藏它的方法!非常感谢! :)
    • 我已经对此事进行了更多搜索,以下代码可能对某些人有所帮助 label2.backgroundColor=[UIColor colorWithPatternImage:image]; [label2 setNeedsDisplay];当您使用 cgrectmake 添加图像时,必须调用 setneeddisplay 。你必须在你的 cellforrowatindexpath 中使用这个代码 NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i-%i", indexPath.section, indexPath.row];我花了可能几个小时希望它对某人有所帮助!有信心!
    猜你喜欢
    • 1970-01-01
    • 2011-12-18
    • 2011-12-24
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多