代码之家  ›  专栏  ›  技术社区  ›  Ketan Odedra ikbal

节中的行计数

  •  0
  • Ketan Odedra ikbal  · 技术社区  · 7 年前

    在我的可扩展的 UITableview 截面数为5 [SectionItems.count] 是的。

    我的目标是对节中的所有单元格进行编号,从1到所有行的计数(编号不应与节相关)。

    enter image description here

    这是我的计数行代码

    NSInteger count = 0;
    for (NSInteger sec=0; sec < indexPath.section; sec++) {
        NSInteger rows = [tableView numberOfRowsInSection:sec];
        count += rows;
    }
    count += indexPath.row + 1;
    
    
    NSArray *sect = [sectionItem objectAtIndex:indexPath.section];
    cell.titleLbl.text = [NSString stringWithFormat:@"%ld %@",(long)count,[sect objectAtIndex:indexPath.row]];
    

    但我得到了你能在下一张图片中看到的东西:

    enter image description here

    问题是第一部分(版本控制方案)有一行,所以这两个数字应该是2和3,而不是1和2。

    我在这里做错什么了?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Yaroslav Luchyt    7 年前

    这里的问题必须是您检查了所有当前可见的行。你应该再创建一个包含单元格编号的数组,然后在得到文本的同时得到它们。

    每次更新行数据时,应重新编号

    - (NSArray *)numberCells {
        NSArray *numbersArray = [[NSArray alloc] init];
        NSInteger num = 1;
        for (NSArray *ar in sectionItem) {
            NSArray *rowArray = [[NSArray alloc] init];
            for (id item in ar) {
                rowArray = [rowArray arrayByAddingObject:[NSNumber numberWithInteger:num]];
                num += 1;
            }
            numbersArray = [numbersArray arrayByAddingObject:rowArray];
        }
        return numbersArray;
    }
    

    需要时更新数组属性,如下所示: myArray = [self numberCells]; 然后得到这样的手机号码:

    NSArray *rowArray = [numbersArray objectAtIndex:indexPath.section];
    NSNumber *num = [rowArray objectAtIndex:indexPath.row];
    

    祝你好运!

        2
  •  1
  •   Alejandro Iván    7 年前

    我会做一些类似于一个节数组的结构,每个节都应该有一个标题和一个行数组。在json风格中,类似于:

    [
        {
            "section_title": "My section 1",
            "section_rows": [
                {"title": "Lecture 1"},
                {"title": "Lecture 2"}
            ]
        },
        {
            "section_title": "My section 2",
            "section_rows": [
                {"title": "Lecture 3"},
                {"title": "Lecture 4"}
            ]
        }
    ]
    

    有了它,你的方法应该是:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return myArray.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        __weak NSDictionary *section = myArray[section];
        return [section[@"section_rows"] count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        __weak NSDictionary *lecture = myArray[indexPath.section][@"section_rows"][indexPath.row];
        // Configure your cell here
    }
    
    // This method should probably be replaced with this one from UITableViewDelegate:
    // - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        __weak NSDictionary *sectionInfo = myArray[section];
        return sectionInfo[@"title"];
    }
    

    在发送/显示数据之前,您应该将其预格式化为易于处理的结构,而不是在尝试计数/访问数据时弄乱。 UIViewController 是的。不要只得到你的 uiview控制器 因为数据太脏,所以它应该是相反的,而您的视图应该是被动的。

    我希望这就是您所要求的,我不太清楚您所说的“可扩展”表视图是什么意思。

    推荐文章