ios 在 tableView 中获取部分 indexPath 的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19650563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the Row for Section indexPath in a tableView
提问by Jared Gross
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. 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. So something with my objectForIndex: method is obviously not right. Any help or references would be super. cheers!
最近我在我的应用程序中为用户名实现了索引功能。我一直在尝试访问每个部分中的行,并在用户点击索引路径时将它们添加到我的收件人数组中。我尝试在 objectAtIndex: 方法中传递许多不同的值,但似乎都没有工作。我能够在 indexPathForRow:inSection: 方法中记录正确的索引行和部分,但返回值是 indexPath 而不是整数。所以我的 objectForIndex: 方法显然是不对的。任何帮助或参考都是超级的。干杯!
This line:
这一行:
PFUser *user = [self.friends objectAtIndex:indexPath.section];
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]);
}
}];
}
采纳答案by Arun
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
}
回答by Jared Gross
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. 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. My final code is here:
使用该代码,我能够为每个部分中的行获取正确的 indexPath。此外,为了使单元格附件复选标记正常工作,我必须使用每个索引的节和行值创建一个 newIndexPath。我的最终代码在这里:
- (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]);
}
}];
}