• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Xcode iOS 仅在本节中添加节和行

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:28:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想添加部分并在最后创建的部分中添加行。

但是当我添加一个部分并添加一行后,它会为所有部分添加行,我只想要最后创建的部分。

我这样做:

@interface SectionTestViewController ()
{
    NSMutableArray *sectionsArray;
    NSMutableArray *rowsInSectionsArray;
    NSInteger currentSection;
}
@end

@implementation SectionTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

    sectionsArray = [[NSMutableArray alloc]init];
    rowsInSectionsArray = [[NSMutableArray alloc]init];

    currentSection = 0;
}

#pragma tableview delegate and datasource methods

-(NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
    return sectionsArray.count;
}

-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    return rowsInSectionsArray.count;
}

-(NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section
{
    return [sectionsArray objectAtIndex:section];
}

添加部分的按钮操作:

- (IBAction)addSectionButtonid)sender {
    [sectionsArray addObject:[NSString stringWithFormat"Section %lu", (unsigned long)sectionsArray.count]];

    currentSection++;

    [self.myTableView reloadData];
}

一个按钮添加行的 Action :

- (IBAction)addRowButtonid)sender {
    [rowsInSectionsArray addObject:[NSString stringWithFormat"Ligne %lu", (unsigned long)rowsInSectionsArray.count]];

    [self.myTableView reloadData];
}

感谢您的帮助



Best Answer-推荐答案


你应该这样做:

@interface SectionTestViewController ()
{
    NSMutableArray *sectionsArrays;
    NSMutableArray *rowsInSectionsArray;
    NSInteger currentSection;
}
@end

@implementation SectionTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

    sectionsArray = [[NSMutableArray alloc]init];
    rowsInSectionsArray = [[NSMutableArray alloc]init];

    currentSection = 0;
}

#pragma tableview delegate and datasource methods

-(NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
    return sectionsArray.count;
}

-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    return [rowsInSectionsArrays objectAtIndex:section].count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [sectionsArray objectAtIndex:section];
}

添加部分的按钮操作:

- (IBAction)addSectionButton:(id)sender {
    [sectionsArray addObject:[NSString stringWithFormat"Section %lu", (unsigned long)sectionsArray.count]];
    [rowsInSectionsArrays addObject:[[NSMutableArray alloc] init]];

    currentSection++;

    [self.myTableView reloadData];
}

在最后一节添加行的按钮操作:

- (IBAction)addRowButton:(id)sender {
    [[rowsInSectionsArray objectAtIndex:currentSection] addObject:[NSString stringWithFormat"Ligne %lu", (unsigned long)[rowsInSectionsArray objectAtIndex:currentSection].count]];

    [self.myTableView reloadData];
}

我刚刚将 rowsInSectionsArray 转换为数组数组。它包含的每个数组都包含一个部分的行。

请务必在 tableView:cellForRowAtIndexPath: 处执行相同操作。

关于ios - Xcode iOS 仅在本节中添加节和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699530/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap