IOS 作業項目 TableView兩個section中cell置頂功能實現


更多技術性文章請關注 合伙呀

點擊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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM