1

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.

Community
  • 1
  • 1
Muthu Sabarinathan
  • 1,198
  • 2
  • 21
  • 49

4 Answers4

4

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.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • Hi Greg - here i have to done all the process in button click event. is it possible to use didselectrowatindexpath. i used ur idea. but its not working. – Muthu Sabarinathan Jan 24 '14 at 10:22
  • You can do it in click event method as well just create NSIndexPath manually and pass it as a parameter: [NSIndexPath indexPathForRow:3 inSection:0]. – Greg Jan 24 '14 at 10:27
  • its working for me. but i used this scrollToRowAtIndexPath in my button click event. – Muthu Sabarinathan Jan 24 '14 at 11:08
  • 1
    @Sabs it works no matter where you put it (tableView delegate or custom methods) – Greg Jan 24 '14 at 11:10
1

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.

avuthless
  • 996
  • 2
  • 12
  • 27
1

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];
}
vignesh kumar
  • 2,310
  • 2
  • 25
  • 39
0

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.

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Palak
  • 46
  • 4