Ios 节中的行数

Ios 节中的行数,ios,objective-c,uitableview,nsindexpath,uitableviewsectionheader,Ios,Objective C,Uitableview,Nsindexpath,Uitableviewsectionheader,在我的可扩展UITableview中,节数为5[SectionItems.count] 我的目标是对节中的所有单元格进行编号,从1到所有行的计数(编号不应考虑节) 这是我的计数行代码 NSInteger count = 0; for (NSInteger sec=0; sec < indexPath.section; sec++) { NSInteger rows = [tableView numberOfRowsInSection:sec]; count += rows

在我的可扩展
UITableview
中,节数为5
[SectionItems.count]

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

这是我的计数行代码

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]];
NSInteger计数=0;
对于(NSInteger sec=0;sec
但我得到了你们可以在下一张图中看到的东西:

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


我在这里做错了什么?

这里的问题一定是您检查了所有当前可见的行。您应该再创建一个包含单元格编号的数组,然后获得与文本相同的编号

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

- (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];

祝你好运

这里的问题一定是您检查了所有当前可见的行。您应该再创建一个包含单元格编号的数组,然后获得与文本相同的编号

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

- (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];

祝你好运

我会做一些类似于节数组的结构,每个节都应该有一个标题和一个行数组。在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"];
}
这样,您的方法将类似于:

[
    {
        "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
上发送/显示数据之前,您应该将数据预格式化为易于处理的结构,而不是在尝试计数/访问数据时弄乱数据。不要因为数据而弄脏你的
UIViewController
,相反,你的视图应该是被动的


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

我会做一些类似于节数组的结构,每个节都应该有一个标题和一个行数组。在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"];
}
这样,您的方法将类似于:

[
    {
        "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
上发送/显示数据之前,您应该将数据预格式化为易于处理的结构,而不是在尝试计数/访问数据时弄乱数据。不要因为数据而弄脏你的
UIViewController
,相反,你的视图应该是被动的


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

您能显示数组的格式吗?。获取对象的视频索引,如果它是单个数组,则显示。能否显示数组的格式?。获取对象的视频索引并显示它是否是单个数组。如预期的完美答案…谢谢。如预期的完美答案…谢谢。