我可以计算每个部分的项目选择数量。
然而,我想知道如何能够在一个部分中跟踪所有部分。
以下rowcount
仅返回当前部分中的项目数,但我还需要跟踪其他部分中的项目数。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger rowcount = [[self.comboTableView indexPathsForSelectedRows] count];
}
顺便说一句,用户可以根据需要选择任意数量的项目。
self.comboTableView.allowsMultipleSelection = YES;
答案 0 :(得分:4)
您可以从所有部分获取所有选定的行。 indexPathsForSelectedRows
将返回表格视图的NSArray<NSIndexPath*>
。不适用于单个部分
- (void)selectedItem: (UITableView *)tableView {
NSArray <NSIndexPath*> *selectedIndexPaths = [tableView indexPathsForSelectedRows];
for (NSIndexPath *indexpath in selectedIndexPaths) {
NSLog(@"Section index : %ld", indexpath.section);
NSLog(@"Row index : %ld", indexpath.row);
}
}
答案 1 :(得分:0)
这是我的自定义逻辑,用于监视当前选定的行。这个逻辑不会影响你当前的任何逻辑。
在.h文件中声明变量,并将init声明为 viewDidLoad 方法。
dicSelectedRow = [NSMutableDictionary new];
在 cellForRowAtIndexPath 和 didSelectRowAtIndexPath mehtod中进行以下更改。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// put your allocation code at here
// To check if current section's row is selected or not...
NSString *strCurrentSection = [NSString stringWithFormat:@"%d",(int)indexPath.section];
NSString *strCurrentIndexPathRow = [NSString stringWithFormat:@"%d",(int)indexPath.row];
NSMutableArray *aryCurrentSelectedRow = [NSMutableArray new];
[aryCurrentSelectedRow addObjectsFromArray:dicSelectedRow[strCurrentSection]];
if(aryCurrentSelectedRow.count > 0)
{
if([aryCurrentSelectedRow containsObject:strCurrentIndexPathRow])
{
// Your row is selected
}
else
{
// Your row is not selected
}
}
aryCurrentSelectedRow = nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here we need to check if row is there into dic or not.
NSString *strCurrentSection = [NSString stringWithFormat:@"%d",(int)indexPath.section];
NSString *strCurrentIndexPathRow = [NSString stringWithFormat:@"%d",(int)indexPath.row];
if(![dicSelectedRow valueForKey:strCurrentSection])
{
// We need to add key first....
[dicSelectedRow setObject:[NSMutableArray new] forKey:strCurrentSection];
}
// Now we need to check if Current Selected Row is there or not.
NSMutableArray *aryCurrentSelectedRow = [NSMutableArray new];
[aryCurrentSelectedRow addObjectsFromArray:dicSelectedRow[strCurrentSection]];
if([aryCurrentSelectedRow containsObject:strCurrentIndexPathRow])
[aryCurrentSelectedRow removeObject:strCurrentIndexPathRow];
else
[aryCurrentSelectedRow addObject:strCurrentIndexPathRow];
if(aryCurrentSelectedRow.count > 0)
{
// If current selection is having value then we replace this to main dic.
[dicSelectedRow setObject:aryCurrentSelectedRow forKey:strCurrentSection];
}
else
{
// Remove key from Dic.
[dicSelectedRow removeObjectForKey:strCurrentSection];
}
NSLog(@"%@",dicSelectedRow.description);
[_tblData reloadData];
}
干杯...
答案 2 :(得分:0)
希望这会有所帮助:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedRows: Int? = tableView.indexPathsForSelectedRows()?.count
var selectedCell: UITableViewCell?
var selectedCellText: String
var selections = [Any]()
for index: IndexPath in tableView.indexPathsForSelectedRows() {
selectedCell = tableView.cellForRow(at: index)
selectedCellText = selectedCell?.textLabel?.text
selections.append(selectedCellText)
}
print("Total selections: \(Int(selectedRows))")
print("selections: \(selections)")
}