OGeek|极客世界-中国程序员成长平台

标题: ios - Xcode iOS 仅在本节中添加节和行 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:28
标题: ios - Xcode iOS 仅在本节中添加节和行

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

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

我这样做:

@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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4