ios - 在 tableView 中获取 Section indexPath 的行

标签 ios indexing row tableview

最近我在我的应用程序中为用户名实现了一个索引功能。我一直在尝试访问每个部分中的行,并在用户点击索引路径时将它们添加到我的收件人数组中。我尝试在我的 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/

相关文章:

ios - UINavigationBar 设置图像未完全绘制

Mysql - 哪个索引更好?

sqlite - 有效匹配索引 SQLite 字段的第一个字符

html - CSS 和 Table/Tr 行大小调整

ios - 如何解析应用程序中的userInfo字典(应用程序: UIApplication, didReceiveRemoteNotification userInfo:[NSObject:AnyObject])

ios - SpriteKit,使用加速度计返回前景时, Sprite 移动得非常快

wpf - 为每个网格行分配边框

PHP 更新现有记录失败

ios - 通过 UITableView 保存到 SQLITE3 时的奇怪行为

python - 列表索引适用于 macOS 但不适用于 Windows?