【发布时间】:2016-04-14 10:55:32
【问题描述】:
是否可以在 UITableView 中获取相应选定行的节数
【问题讨论】:
-
NSIndexPath包含section和row信息。
标签: ios uitableview
是否可以在 UITableView 中获取相应选定行的节数
【问题讨论】:
NSIndexPath 包含section 和row 信息。
标签: ios uitableview
是的,您可以使用
检查部分indexPath.section
【讨论】:
如果你有 NSIndexPath,你可以得到 row by
indexPath.row
或部分
indexPath.section
【讨论】:
试试下面的示例代码:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows = 0;
switch (section) {
case 0:
rows = 7;
break;
case 1:
rows = 21;
break;
case 2:
rows = 3;
break;
case 3:
rows = 1;
break;
case 4:
rows = 5;
break;
default:
break;
}
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:@"section : %ld & rows : %ld",(long)indexPath.section,(long)indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//This will print which row of which section is clicked
NSLog(@"section : %ld & rows : %ld",(long)indexPath.section,(long)indexPath.row);
}
【讨论】:
这是对的!
NSArray <NSIndexPath *>*rows = [tableView indexPathsForSelectedRows];
【讨论】:
我解决了我正在寻找的问题。我想在单击按钮时获取其中各个选定行的所有部分编号。
我用过
NSArray *paths = [self.tableView indexPathsForSelectedRows];
【讨论】: