更多技术性文章请关注 合伙呀

点击cell会置顶,其他的下移

第一,引入代理

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

第二,实现

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.itemList = [[NSMutableArray alloc] init];
    //存放置顶数据的数组
    NSMutableArray *topArray = [[NSMutableArray alloc] init];
    //存放不置顶数据的数组
    NSMutableArray *normalArray = [[NSMutableArray alloc] init];
    [self.itemList addObject:topArray];
    [self.itemList addObject:normalArray];
    
    [normalArray addObject:@"1"];
    [normalArray addObject:@"2"];
    [normalArray addObject:@"3"];
    [normalArray addObject:@"4"];
    [normalArray addObject:@"5"];
    [normalArray addObject:@"6"];
    [normalArray addObject:@"7"];
    [normalArray addObject:@"8"];
    [normalArray addObject:@"9"];
    [normalArray addObject:@"10"];
    [normalArray addObject:@"11"];
    [normalArray addObject:@"12"];
    
    
    CGRect frame = {0,20,320,460};
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//设置一共有2个分区,分别做为置顶区与正常区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //根据分区号,获取对应的存放容器
    NSArray *itemArray = [self.itemList objectAtIndex:section];
    return itemArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根据分区号,获取对应的存放容器
    NSArray *itemArray = [self.itemList objectAtIndex:indexPath.section];
    //指定置顶区的唯一标识符..
    static NSString *topIdentifier = @"topIdentifier";
    //指定正常区的唯一标识符
    static NSString *normalIdentifier = @"normalIdentifier";
    
    //声明返回的变量名
    UITableViewCell *cell = nil;
    
    switch (indexPath.section) {
        case 0://置顶区域
        {
            cell = [tableView dequeueReusableCellWithIdentifier:topIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topIdentifier];
            }
            cell.textLabel.text = itemArray[indexPath.row];
            break;
        }
        case 1://正常区域
        {
            cell = [tableView dequeueReusableCellWithIdentifier:normalIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalIdentifier];
            }
            cell.backgroundColor = [UIColor whiteColor];
            cell.textLabel.text = itemArray[indexPath.row];
        }
    }
    NSLog(@"22222222");
    //返回单元格
    return cell;
}

//通过点击单元格完成置顶操作...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果点击的是置顶分区则直接返回..
    if (indexPath.section == 0) {
        return;
    }
    //否则,则点击的是正常分区,开始整理数据...
    NSMutableArray *topArray = [self.itemList objectAtIndex:0];
    NSMutableArray *normalArray = [self.itemList objectAtIndex:1];
    
    //正常区域的点击数据
    NSString *item  = [normalArray objectAtIndex:indexPath.row];
    
    [tableView beginUpdates];//数据数据改变数据,tableview做相应的刷新
    //当置顶区域的置顶条数大于等于3时,将置顶区域的最后一个元素移除并且添加到正常区域的第一条
    if (topArray.count >= 3)
    {
        //置顶区域的最后一条数据
        NSString *lastTopItem  = [topArray lastObject];
        [normalArray insertObject:lastTopItem atIndex:0];
        [topArray removeObject:lastTopItem];
        //第二行
        NSIndexPath *lastTopIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
        NSIndexPath *headNormalIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
        [tableView moveRowAtIndexPath:lastTopIndexPath toIndexPath:headNormalIndexPath];
    }
    //当置顶区域的置顶条数小于3时,直接添加进置顶数组,并且从正常区域移除该对象...
    [topArray insertObject:item atIndex:0];
    [normalArray removeObject:item];
    
    NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [tableView moveRowAtIndexPath:indexPath toIndexPath:topIndexPath];
    
    [tableView endUpdates];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;
    switch (section) {
        case 0:
            title = @"置顶的啊";
            break;
        case 1:
            title = @"普通的啊";
        default:
            break;
    }
    return title;
}