Ios 用于循环存储带有附件图标的行到阵列
我需要一个for循环的帮助,我还没有完全正确。我花了太长时间在这件事上大惊小怪,我想我应该求助于专家 我需要它来遍历每个索引路径并检查附件图标。如果是复选标记,请将其添加到数组中,然后最后记录数组 这一切都与按钮动作有关。代码如下:Ios 用于循环存储带有附件图标的行到阵列,ios,iphone,objective-c,for-loop,Ios,Iphone,Objective C,For Loop,我需要一个for循环的帮助,我还没有完全正确。我花了太长时间在这件事上大惊小怪,我想我应该求助于专家 我需要它来遍历每个索引路径并检查附件图标。如果是复选标记,请将其添加到数组中,然后最后记录数组 这一切都与按钮动作有关。代码如下: //Handles tableView row selection and addition and removal of the checkmark - (void)tableView:(UITableView *)theTableView didSelectRo
//Handles tableView row selection and addition and removal of the checkmark
- (void)tableView:(UITableView *)theTableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [theTableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//Reflect selection in data model
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
//Reflect deselection in data model
}
}
//Sends checkmarked items to console log
- (IBAction)sendResults:(id)sender {
NSMutableArray *aList = [[NSMutableArray alloc] init];
//This is the part that isn't quite right
for (NSIndexPath *indexPath in _tableView.indexPathsForSelectedRows) {
NSString *r = [NSString stringWithFormat:@"%li",(long)indexPath.row];
[aList addObject:r];
}
NSLog(@"Selected Items: %@", _aList);
}
我认为问题在于没有选择任何表视图单元格。在我的didSelectRowAtIndexPath
方法中,我将附件图标选中一个复选标记,但随后它似乎取消了对该行的选择。因此,当我在日志中记录时,\u tableView.indexPathsForSelectedRows
中没有对象(我在日志中得到“Selected Items:(null)”
这个问题是有道理的,但我似乎无法解决。有人能帮忙吗?谢谢!在
tableView:didSelectRowAtIndexPath:
中,您正在取消选择行。因此,当您的循环在所选行上执行时(\u tableView.indexPathsForSelectedRows)没有条目。在该行旁边显示复选标记与让UITableView实际将其识别为选中不同
若要解决此问题,请删除
[表格视图取消选中索引路径:[theTableView indexPathForSelectedRow]动画:否];
或将循环更改为测试cell.accessoryType=UITableViewCellAccessoryCheckmark
在检查附件类型之前取消选中该行