Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

objective c - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...