[英]Getting the Row for Section indexPath in a tableView
最近我在我的應用程序中為用戶名實現了索引功能。 我一直在嘗試訪問每個部分中的行,並在用戶點擊索引路徑時將它們添加到我的收件人數組中。 我嘗試在objectAtIndex:方法中傳遞許多不同的值,但似乎都沒有工作。 我能夠在indexPathForRow:inSection:方法中記錄正確的索引行和節,但返回值是indexPath而不是整數。 所以使用我的objectForIndex:方法顯然不對。 任何幫助或參考都是超級的。 干杯!
這一行:
PFUser *user = [self.friends objectAtIndex:indexPath.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]);
}
}];
}
嗨,這是足夠的嘗試,讓我知道你是否面臨任何問題...
- (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
}
就像代碼Spynet建議的只是有點不同:
NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
PFUser *user = [array objectAtIndex:indexPath.row];
使用該代碼,我能夠為每個部分中的行獲取正確的indexPath。 此外,為了使單元格附件檢查標記正常工作,我必須使用每個索引的section和row值創建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]);
}
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.