tags:

views:

18

answers:

2

Hello everyone

I put a DragView (subclass of UIView) on a UITableViewCell with tag (row+20000)

- (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;
}

But when I try to use the codes (line marks with 'error') to access the DragView, debugger shows that newDragger returns 0x0 which means no object.

I do not know where is wrong. I just guess there may be the reason of the amount limitation of maximium tag )

Welcome any comment.

Thanks

interdev

+1  A: 

tag property has NSInteger type and can have any value NSInteger can hold, so 20000 should not cause problems.

Code you posted definitely lacks some details - do you actually put dragger into your cell? Note also that it is recommended not to add subviews to UITableViewCell directly - you must add them to cell's contentView instead:

[cell.contentView addSubView: dragger];

and access it later:

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

Also note that your code will not work when table reuses the cells, that is when cell is used for a row different from the row it was initially created for and you will try to access subview with wrong tag anyway.

P.S. Also see this question http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview/1802875#1802875. It deals with buttons but may be it will be helpful for your custom view as well.

Vladimir
A: 

The cell may be come from dequeueReusableCellWithIdentifier: method, and this cell's previous row number may be someone else. So, the tag cannot be trusted.

Toro