代码之家  ›  专栏  ›  技术社区  ›  arachide

UITableViewCell上视图标记的最大限制是什么

  •  0
  • arachide  · 技术社区  · 14 年前

    我在带有标记(行+20000)的UITableViewCell上放置了一个DragView(UIView的子类)

    - (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {   
    
       NSInteger row=[indexPath row];
       NSInteger section=[indexPath section];
       static NSString *SimpleTableIdentifier1 = @"CellTableIdentifier";
       UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier1 ];
       if  (cell == nil) {
          cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero 
          CGRect dragRect =CGRectMake(12.0f,6.0f,24.0f,24.0f);
          DragView *dragger =[[DragView alloc]  initWithFrame:dragRect];
          [dragger setTag: row+20000 ];
    
       }
       DragView *newDragger=(DragView*)[cell viewWithTag: row+20000 ];//error
    
       //......
       return cell;
    }
    

    但当我尝试使用代码(带有“错误”的行标记)访问DragView时,调试器显示NewDragger返回0x0,这意味着没有对象。

    我不知道哪里出了问题。我只是想可能是因为Maximium标签的数量有限)

    欢迎发表评论。

    谢谢

    交互开发

    2 回复  |  直到 14 年前
        1
  •  1
  •   Community CDub    8 年前

    标记属性具有nsinteger类型,并且可以具有nsinteger可以保留的任何值,因此20000不应导致问题。

    你发布的代码肯定缺少一些细节——你真的把Dragger放到你的手机里了吗?还请注意,建议不要将子视图直接添加到UITableViewCell中-必须将它们添加到单元格的ContentView中:

    [cell.contentView addSubView: dragger];
    

    稍后访问:

    DragView *newDragger=(DragView*)[cell.contentView viewWithTag: row+20000 ];
    

    另外请注意,当表重用单元格时,代码将不起作用,也就是说,当单元格用于与最初为其创建的行不同的行时,您将尝试使用错误的标记访问子视图。

    P.S.也看到这个问题 Detecting which UIButton was pressed in a UITableView . 它处理按钮,但可能对您的自定义视图也有帮助。

        2
  •  0
  •   AechoLiu    14 年前

    单元格可能来自dequeuersabellWithIdentifier:方法,并且此单元格的前一个行号可能是其他人。因此,标签不能被信任。