IOS 构造和使用TableView(基于storyboard) (七)移动cell和section

本文深入探讨了iOS开发领域的关键技术和实践,包括Swift语言、Xcode开发环境、UIKit框架以及移动应用的设计与实现。重点阐述了如何利用Objective-C和Swift构建高效、稳定的iOS应用,同时介绍了移动应用开发中的最佳实践和常见问题解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据源的加载

我们加载3个Section,每个section有3个Cell

从视图头文件开始

#import <UIKit/UIKit.h>

@interface myTableViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *myTable;
@property (nonatomic, strong) NSMutableArray *arrayOfSections;
@end
定义一个数据填充方法

- (NSMutableArray *) newSectionWithIndex:(NSUInteger)paramIndex withCellCount:(NSUInteger)paramCellCount
{
    NSMutableArray *result = [[NSMutableArray alloc] init];
    NSUInteger counter = 0;
    for (counter = 0; counter < paramCellCount; counter++) {
        [result addObject:[[NSString alloc]initWithFormat:@"Section %lu Cell %lu",(unsigned long)paramIndex,(unsigned long)counter+1]];
    }
    return result;
}
初始化

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    arrayOfSections = [[NSMutableArray alloc]init];
    NSMutableArray *section0 = [self newSectionWithIndex:1 withCellCount:3];
    NSMutableArray *section1 = [self newSectionWithIndex:2 withCellCount:3];
    NSMutableArray *section2 = [self newSectionWithIndex:3 withCellCount:3];
    [arrayOfSections addObject:section0];
    [arrayOfSections addObject:section1];
    [arrayOfSections addObject:section2];
}
定义Section数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger result = 0;
    if ([tableView isEqual:self.myTable]) {
        result = (NSInteger)[self.arrayOfSections count];
    }
    return result;
}
定义row数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger result = 0;
    if ([tableView isEqual:self.myTable]) {
        if ([self.arrayOfSections count]>section) {
            NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:section];
            result = (NSInteger)[sectionArray count];
        }
    }
    return result;
}
填充数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *result = nil;
    if ([tableView isEqual:self.myTable]) {
        static NSString *TableViewCellIdentifier = @"MyCells";
        result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
        if (result == nil) {
            result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];
        }
        NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:indexPath.section];
        result.textLabel.text = [sectionArray objectAtIndex:indexPath.row];
    }
    return result;
}
数据填充完毕


移动Section主要是通过调用moveSection:toSection:方法

 - (void) moveSection:(NSInteger)S1 ToSection:(NSInteger)S2
{
    NSMutableArray *section = [self.arrayOfSections objectAtIndex:0];
    [self.arrayOfSections removeObject:section];
    [self.arrayOfSections insertObject:section atIndex:S2];
    [self.myTable moveSection:S1 toSection:S2];
} 
在选中某个Cell时执行移动

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.myTable]) {
        [self moveSection:1 ToSection:0];
    }
}
效果如下

 

移动Cell

- (void)moveCell:(NSInteger)C1 InSection:(NSInteger)S1 ToCell:(NSInteger)C2 InSection:(NSInteger)S2
{
    NSMutableArray *section1 = [self.arrayOfSections objectAtIndex:S1];
    NSMutableArray *section2 = [self.arrayOfSections objectAtIndex:S2];
    NSString *cell2InSection1 = [section1 objectAtIndex:C2];
    [section1 removeObject:cell2InSection1];
    [section2 insertObject:cell2InSection1 atIndex:C1];
    NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:C1 inSection:S1];
    NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:C2 inSection:S2];
    [self.myTable moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
点击Cell时触发

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.myTable]) {
        [self moveCell:0 InSection:0 ToCell:0 InSection:1];
    }
}
效果如下

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值