【问题标题】:Get UITableviewCell position from "visible" area or window从“可见”区域或窗口获取 UITableviewCell 位置
【发布时间】:2011-08-21 01:23:53
【问题描述】:

我基于 uitableview 和 uitabbar 创建了一个 iPhone 应用程序。在 tableview 的每个单元格上,我都有一个“添加到收藏夹按钮”。 当我按下这个按钮时,我想让单元格从她的位置“跳转”到标签栏的最喜欢的项目(与 installous 中的下载效果相同)

如果我在前 10 个单元格上效果很好,但如果我在 tableView 中滚动效果不好,因为起始位置是根据 uitableview 大小计算的。

可以从可见区域知道单元格的位置。 ?

示例:对于第 40 个单元格位置是 x:0,y:1600,w:320,y:50,我想要从屏幕顶部开始的位置而不是从 uiview 顶部开始的位置,所以像这样 x: 0,y:230,w:320,y:50

【问题讨论】:

    标签: objective-c uitableview position cell


    【解决方案1】:

    是的,您可以使用rectForRowAtIndexPath:,您只需传入您单击的行的 indexPath。

    rectForRowAtIndexPath:

    返回一行的绘图区域 由索引路径标识。

    ...

    编辑

    转化率:

    CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
    CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]]; 
    

    【讨论】:

    • 对于 20 个单元格的 RectForRowAtIndexPath 返回我 x:0,y:1200 并且我想要视图顶部的位置(从状态栏)所以它应该在 20 到大约 367 之间(视图的大小)
    • @Benjamin Frolicher 抱歉,我已经更新了答案,缺少转换。
    • 谢谢尼克,让我免于尝试做一些时髦的数学运算,添加所有 tableview 组件以获得选定的行位置。
    • 谢谢你到处找这个。
    【解决方案2】:

    所以这是我使用的代码:(我敢肯定它可以优化):

    首先将 NSindexPath ** 属性添加到您的自定义单元格中。
    *在你的 tableview 控制器中实现这个:*

    -(void)addCellToFavorisWithAnimation:(ResultSearchOffreCell*)cell{
    /* Generate image from cell
    ----------------------------------------------------------*/
    UIGraphicsBeginImageContextWithOptions(cell.bounds.size, cell.opaque, [[UIScreen mainScreen] scale]);
    [cell.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // Launch first animation (go to top-right)
    //----------------------------------------------------------*/
    CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:cell.indexPath];
    CGRect rectInSuperview = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];
    
    cellAnimationContainer = [[UIImageView alloc] initWithFrame:rectInSuperview];
    
    [cellAnimationContainer setFrame:CGRectMake(cellAnimationContainer.frame.origin.x, cellAnimationContainer.frame.origin.y+50 , cellAnimationContainer.frame.size.width, cellAnimationContainer.frame.size.height)];
    
    [cellAnimationContainer setImage:img];
    
    AppDelegate_Shared *appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.window addSubview:cellAnimationContainer];
    [UIView beginAnimations:@"addFavorite-Step1" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [cellAnimationContainer setFrame:CGRectMake(20,cellAnimationContainer.frame.origin.y-10, cellAnimationContainer.frame.size.width, cellAnimationContainer.frame.size.height)];
    [cellAnimationContainer setAlpha:0.7];
    [UIView commitAnimations];
    }
    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    
    /* Launch second animation => go to favoris Tab and remove from self.view
     ---------------------------------------------------------------------------*/
    if([anim isEqual:@"addFavorite-Step1"])
    {
        [UIView beginAnimations:@"addFavorite-Step2" context:nil]; 
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationDelegate:self]; 
        [cellAnimationContainer setAlpha:0.4];
        [cellAnimationContainer setFrame:CGRectMake(150, 420, 20, 20)];
        [UIView commitAnimations];
    }
    else if([anim isEqual:@"addFavorite-Step2"])
    {
        [cellAnimationContainer removeFromSuperview];
        [cellAnimationContainer release];         
    }
    }
    

    【讨论】:

      【解决方案3】:

      是的,这是可能的

      -(void)click:(id)sender
      {
          int clickcelltag;
          clickcelltag=sender.tag;
      
          NSIndexPath *index =[NSIndexPath indexPathForRow:clickcelltag inSection:0];
      
          [userreviewtblview scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];//userreviewtblview is a UITableView name
      }
      

      希望这会对你有所帮助:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-15
        • 2012-12-03
        • 1970-01-01
        • 1970-01-01
        • 2011-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多