In iOS selected cell should move to top portion in UITableView

后端 未结 4 510
后悔当初
后悔当初 2020-12-20 07:05

I have more than 20 cells in my custom table view, in execution time 6 cells will be visible. Now i select the 4 th cell means, that 4th cell have to come in first position

相关标签:
4条回答
  • 2020-12-20 07:41

    As i wrote in the comments:

    Upon selection, move selected arr_CalendarTitle entry to the top of array and call reloadData() on tableView. Table view displays data as is sorted in arr_CalendarTitle.

    moveRowAtIndexPath is not enough, must resort the array too.

    So, before reloadData() call (in button click method), do this:

    id object = [[[arr_CalendarTitle objectAtIndex:int_SelectedIndex] retain] autorelease];
    [arr_CalendarTitle removeObjectAtIndex:int_SelectedIndex];
    [arr_CalendarTitle insertObject:object atIndex:0];
    

    For ARC you can use :

    __autoreleasing id object = [arr_CalendarTitle objectAtIndex:int_SelectedIndex];
    [arr_CalendarTitle removeObjectAtIndex:int_SelectedIndex];
    [arr_CalendarTitle insertObject:object atIndex:0];
    

    Since you have more than one array that holds data (only noticed that now) you must do this for every array thats holds data for tableView cells.

    0 讨论(0)
  • 2020-12-20 07:52

    In the method_Expand method after fetching the selected row you have to remove the object at the selected index and insert the removed object at 0 index.

    Also you want to move the move next item to the second position

    For that you have to increment the selected index and check if that index is with in the array bounds then remove that item and add it to the index 1;

    - (void)method_Expand:(UIButton*)sender
    
        UITableViewCell *cell = (UITableViewCell *)sender.superview;
        NSIndexPath *indexpath = [tbl_CalendarList indexPathForCell:cell];
    
        int nextIndex=indexpath.row+1;
    
        // first remove the object
       NSString *str=[arr_CalendarTitle objectAtIndex];
       [arr_CalendarTitle removeObjectAtIndex:indexpath.row];
       [arr_CalendarTitle insertObject:str atIndex:0];
    
       //do the same to arr_CalendarEventTime,arr_CalendarDate,arr_CalendarMont etc
    
      if([arr_CalendarTitle count]-1<nextIndex)// check if nextIndex within bounds to avoid crash
      {
       // remove next object and addit to the index 1 to all array
      }
       [tbl_CalendarList reloadData];
    }
    
    0 讨论(0)
  • 2020-12-20 07:53

    Use below method to scroll your tableview.

    [tableview setContentOffset:CGPointMake(0, button.tag*tableview.rowHeight) animated:YES];
    

    This method will make tableview scroll to specified point. Change value of Y in CGPointMake according to your code.

    0 讨论(0)
  • 2020-12-20 07:54

    You can scroll your table view to that cell and you can specify that you want to scroll it on top when you select the cell:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    

    Hope this is what you are after.

    0 讨论(0)
提交回复
热议问题