选定的单元格应该移动到UITableView的顶部

选定的单元格应该移动到UITableView的顶部

本文介绍了在iOS中,选定的单元格应该移动到UITableView的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义表格视图中有超过20个单元格,在执行时间内将显示6个单元格。现在我选择第4个单元意味着,第4个单元必须进入第一个位置,第5个单元进入第2个位置,依此类推。怎么做这个过程,我试过这样。

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事件调用

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

调整单元格大小

- (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.

当我按下它时就是这样。

when i pressed it comes like this.

此选定的单元格应该位于第一位置。

This selected cell should come to first position.

推荐答案

您可以将表格视图滚动到该单元格,并且可以指定在选择单元格时将其滚动到顶部:

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.

这篇关于在iOS中,选定的单元格应该移动到UITableView的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:48