2014-01-19 36 views
0

需要关于for循环的帮助,我还没有完全弄清楚。过了很长时间,我想我应该转向专家。For Loop将存储行的附件图标存储到阵列

我需要这个遍历每个索引路径并检查附件图标。如果它是复选标记,请将其添加到数组中,最后记录数组。

这一切都与一个按钮操作绑定。这里是代码:

//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方法中,我将附件图标检查为复选标记,但似乎取消选中该行。所以当我登录时我在_tableView.indexPathsForSelectedRows没有任何对象(我在日志中得到“Selected Items:(null)”。

这个问题很有意义,但我似乎无法解决这个问题。 ?谢谢!

+0

您在检查附件类型之前取消选择该行 – Joshua

回答

0

tableView:didSelectRowAtIndexPath:因为你没有选择该行,所以当你的循环在选定的行(_tableView.indexPathsForSelectedRows)上执行时,没有条目,在行旁边显示一个复选标记与使UITableView实际识别不同它被选中

要解决此问题或者删除[theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];或更改您的循环以测试cell.accessoryType = UITableViewCellAccessoryCheckmark