我有一个包含两个UIButton属性(nameLabel和profileImageButton)的单元格,用于查看帖子的用户个人资料页面。我试图从当前单元格中获取用户,但未调用didSelectRowAtIndex。我意识到这是假设发生(没有被调用的方法),但我不知道如何解决它。
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
postCell *cell = (postCell *)[tableView dequeueReusableCellWithIdentifier:@"postCell"];
cell.personStringPost.text = [object objectForKey:@"stringPost"];
[cell.nameLabel setTitle:[object objectForKey:@"User_Name"] forState:UIControlStateNormal];
cell.userId = [object objectForKey:@"userId"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
PFFile *imageFile = [object objectForKey:@"profileImage"];
NSData *data = [imageFile getData];
cell.profileImage.image = [UIImage imageWithData:data];
cell.nameLabel.tag = indexPath.row;
[cell.nameLabel addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[cell.profileImageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
postCell *cell = (postCell *)[tableView cellForRowAtIndexPath:indexPath];
self.userId = cell.userId;
NSLog(@"Did Select: %@", self.userId);
}
- (void) buttonPressed: (id) sender{
UIButton *button = (UIButton *)sender;
NSInteger row = button.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self performSegueWithIdentifier:@"personProfile" sender:self];
}
答案 0 :(得分:-1)
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
//选择和取消选择行。这些方法will not call
委托方法(-tableView:willSelectRowAtIndexPath:或tableView:didSelectRowAtIndexPath :),也不会发出通知。
所以用以下两行替换buttonPressed中的selectRowAtIndexPath
:
postCell *cell = (postCell *)[tableView cellForRowAtIndexPath:indexPath];
self.userId = cell.userId;