简体   繁体   English

在tableView中获取section index的行

[英]Getting the Row for Section indexPath in a tableView

Recently I put implemented an indexing feature for the user names in my app. 最近我在我的应用程序中为用户名实现了索引功能。 I have been trying to access the rows in each section and add them to my recipients array when the user taps the index path. 我一直在尝试访问每个部分中的行,并在用户点击索引路径时将它们添加到我的收件人数组中。 I have tried passing many different values in my objectAtIndex: method but none seem to be working. 我尝试在objectAtIndex:方法中传递许多不同的值,但似乎都没有工作。 I am able to log the correct index row and section in indexPathForRow:inSection: method but the return value is an indexPath and not an integer. 我能够在indexPathForRow:inSection:方法中记录正确的索引行和节,但返回值是indexPath而不是整数。 So something with my objectForIndex: method is obviously not right. 所以使用我的objectForIndex:方法显然不对。 Any help or references would be super. 任何帮助或参考都是超级的。 cheers! 干杯!

This line: 这一行:

PFUser *user = [self.friends objectAtIndex:indexPath.section];

Returns only the first username in each section, no matter what name I tap in the section. 无论我在该部分中使用什么名称,都只返回每个部分中的第一个用户名。 So I need to access the Row in addition, not just the section. 所以我需要另外访问Row,而不仅仅是部分。

- (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]);
    }
}];
}

Hi this is enough try and let me know if you face any issues... 嗨,这是足够的尝试,让我知道你是否面临任何问题...

- (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
}

Just like the code Spynet suggested only a little bit different: 就像代码Spynet建议的只是有点不同:

NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
    PFUser *user = [array objectAtIndex:indexPath.row];

Using that code I was able to get the proper indexPath for row in each section. 使用该代码,我能够为每个部分中的行获取正确的indexPath。 Also, in order to get the cells accessory checkmarks to work correctly I had to create a newIndexPath using the section and row values for each index. 此外,为了使单元格附件检查标记正常工作,我必须使用每个索引的section和row值创建newIndexPath。 My final code is here: 我的最终代码在这里:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    int row = indexPath.row;
    int section = indexPath.section;

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
    NSLog(@"newIndexPath: %@", newIndexPath);

    [self.tableView deselectRowAtIndexPath:newIndexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];

    NSLog(@"sectionsArray:%@",self.sectionsArray);

    NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
    PFUser *user = [array objectAtIndex:indexPath.row];
    NSLog(@"array:%@",array);
    NSLog(@"user:%@",user);

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user];
        NSLog(@"recipients:%@",self.recipients);
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user];
        NSLog(@"recipients:%@",self.recipients);
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM