最近我在我的应用程序中为用户名实现了一个索引功能。我一直在尝试访问每个部分中的行,并在用户点击索引路径时将它们添加到我的收件人数组中。我尝试在我的 objectAtIndex: 方法中传递许多不同的值,但似乎没有一个有效。我能够在 indexPathForRow:inSection: 方法中记录正确的索引行和部分,但返回值是 indexPath 而不是整数。所以我的 objectForIndex: 方法显然不对。任何帮助或引用都会很棒。干杯!
这一行:
PFUser *user = [self.friends objectAtIndex:indexPath.section];
无论我在该部分中点击什么名称,都只返回每个部分中的第一个用户名。所以我还需要访问行,而不仅仅是部分。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
int section = indexPath.section;
int row = indexPath.row;
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
NSLog(@"newIndexPath: %@", newIndexPath);
PFUser *user = [self.friends objectAtIndex:indexPath.section];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId];
NSLog(@"added:%@",user);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId];
NSLog(@"removed:%@",user);
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
}];
}
最佳答案
嗨,这就足够了,如果您遇到任何问题,请告诉我...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray * nameArray = [self.sectionsArray objectAtIndex:indexPath.section];
PFUser *user = (PFUser*)[nameArray objectAtIndex:indexPath.row];
// PFUser *user = (PFUser*)[self.friends objectAtIndex:indexPath.section];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId];
NSLog(@"added:%@",user);
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId];
NSLog(@"removed:%@",user);
}
// this is enough
}
关于ios - 在 tableView 中获取 Section indexPath 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19650563/