Muthu Sabarinathan
Muthu Sabarinathan

Reputation: 1208

In iOS selected cell should move to top portion in UITableView

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 and 5th cell in 2nd position and so on. how to do this process, i tried like this.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MACalendarCustomCell *cell = (MACalendarCustomCell*) [tableView dequeueReusableCellWithIdentifier:[MACalendarCustomCell reuseIdentifier]];
    if(cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"MACalendarCustomCell" owner:self options:nil];
        cell = customCell;
        customCell = nil;
        
    }
    
        cell.lbl_CalendarTitle.text = [arr_CalendarTitle objectAtIndex:indexPath.row];
        cell.lbl_CalendarSubTitle.text = [arr_CalendarSubTitle objectAtIndex:indexPath.row];
        cell.lbl_calendarEventTime.text = [arr_CalendarEventTime objectAtIndex:indexPath.row];
        cell.lbl_calendarDate.text = [arr_CalendarDate objectAtIndex:indexPath.row];
        cell.lbl_CalendarMonth.text = [arr_CalendarMonth objectAtIndex:indexPath.row];
        cell.lbl_CalendarYear.text = [arr_CalendarYear objectAtIndex:indexPath.row];
        cell.img_BackGround.image = [UIImage imageNamed:@"calendar_Cell_Up.png"];
        
        //here click event is occurred.
        cell.btn_CollapseExpand.tag = indexPath.row;
        [cell.btn_CollapseExpand addTarget:self action:@selector(method_Expand:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}

ButtonPressed event calls

- (void)method_Expand:(UIButton*)sender
{
    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexpath = [tbl_CalendarList indexPathForCell:cell];
    
    [tbl_CalendarList moveRowAtIndexPath:[NSIndexPath indexPathForItem:indexpath.row inSection:indexpath.section] toIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexpath.section]];
    
    int_SelectedIndex = sender.tag;
    NSLog(@"Selected Button : %ld",(long)int_SelectedIndex);
    if ( int_TempSelectedIndex != int_SelectedIndex)
    {
        int_TempSelectedIndex = int_SelectedIndex;
    }
    else
    {
        int_TempSelectedIndex = -1;
    }
   
    [tbl_CalendarList reloadData];
}

Resizing the cell

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == int_TempSelectedIndex )
    {
        cellSize = 300;
        isRowSelected[indexPath.row] = YES;
    }
    else
    {
        cellSize = 100;
        isRowSelected[indexPath.row] = NO;
    }

    return cellSize;
    
}

Now i got Like this in simulator.

enter image description here

when i pressed it comes like this.

enter image description here

This selected cell should come to first position.

Upvotes: 1

Views: 3563

Answers (4)

Palak
Palak

Reputation: 46

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.

Upvotes: 0

avuthless
avuthless

Reputation: 996

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.

Upvotes: 1

vignesh kumar
vignesh kumar

Reputation: 2330

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

Upvotes: 1

Greg
Greg

Reputation: 25459

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.

Upvotes: 4

Related Questions