我明白这是假设发生,但是当按钮被点击的方法被调用,而是选择了错误的电池我一直没能找到一种方法来调用此方法。
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
_postCell = (postCell *) [self.tableView dequeueReusableCellWithIdentifier:@"postCell"];
_postCell.personStringPost.text = [object objectForKey:@"stringPost"];
[_postCell.nameLabel setTitle:[object objectForKey:@"User_Name"] forState:UIControlStateNormal];
_postCell.userId = [object objectForKey:@"userId"];
_postCell.selectionStyle = UITableViewCellSelectionStyleNone;
PFFile *imageFile = [object objectForKey:@"profileImage"];
NSData *data = [imageFile getData];
_postCell.profileImage.image = [UIImage imageWithData:data];
[_postCell.nameLabel addTarget:self action:@selector(personProfile:tableView:) forControlEvents: UIControlEventTouchUpInside];
[_postCell.profileImageButton addTarget:self action:@selector(personProfile:tableView:) forControlEvents:UIControlEventTouchUpInside];
return _postCell;
}
- (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) personProfile: (NSIndexPath *) indexPath tableView: (UITableView *) tableView{
[self tableView:tableView didSelectRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"personProfile" sender:self];
}
几件事我希望这将有助于
在处理按钮只有一个节
在cellfor排方法
yourcell.mybutton.tag=indexPath.row;
-(void)myCellbuttonAction:(id)sender
{
UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton
NSInteger row = button.tag; // recover the row
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
// apply your logic for indexpath as in didselect row
}
同时处理多个节和多行它,这可能帮助你
在用于行方法细胞
yourcell.tag=indexPath.section;
yourcell.contentview.tag=indexPath.row;
和您的按钮操作可能是这样的
-(void)myCellbuttonAction:(id)sender
{
UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton
id rowid =[button superview];
id sectionid = [rwoid superview];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[rowid tag] inSection:[sectionid tag]];
// apply your logic for indexpath as in didselect row
}
这里有几件事情。
一)在cellForRowAtIndex方法我不会用一个实例变量(_postCell)。 你可能有电池再利用的问题,这很可能是你错误的根源。 与局部变量替换为:
postcell *cell = (postCell *)[self.tableView dequeueReusableCellWithIdentifier:@"postCell"];
您还需要更换所有引用_postCell
与cell
。
b)注意到在同一行的投用小写= (postCell *)...
-我也做了同样的上方,但它是最好的做法类名称以大写字母开头。
c)你有一个命名的属性nameLabel
,这表明它是一个UILabel
,但您使用setTitle:forState:
方法,这意味着它是一个UIButton
。 由于调试将是一个容易得多,如果名称相匹配的类(或至少不意味着错误的类),我会重新命名这个属性。
d)当你调用addTarget:action:forControlEvents
方法,您的选择是personProfile:tableView:
该方法的签名是用于NSIndexPath和一个UITableView。 但是,你的按钮将不会被发送这些类型的那些参数。 它会发送发件人的细节 - 即其触发该操作的按钮。 所以,你需要修改你的方法接受类型的参数:
[cell.nameLabel addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
e)如果该方法被调用,您需要一种方法来确定发送按钮是哪个小区。 理想情况下,你会继承UIButton的一些链接添加到细胞,但(如果你只有一个部分)则可能是把行号作为一个标签就完事。 要做到这一点添加:
cell.nameLabel.tag = indexPath.row;
你cellForRowAtIndexPath:
然后你就可以实现不同的方法来处理按下按钮,如下所示:
-(void)buttonPressed:(id)sender {
UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton
NSInteger row = button.tag; // recover the row
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; // derive the indexPath, assuming section is 0
[self.tableView selectRowAtIndexPath:indexPath animated:YES]; // select the relevant row in the table (assuming the table is self.tableView)
[self performSegueWithIdentifier:@"personProfile" sender:self]; // perform the segue
}
f)在上述需要注意的是,你不应该调用 tableView:didSelectRowAtIndexPath:
。 这是当用户选择的行,而不是为程控选择行的tableView调用。 使用selectRowAtIndexPath:animated:
代替。
这基本上是pbasdf与斯威夫特3以上回答
我叫didSelectRowAt直接,因为它不是由selectRowAtIndexPath触发
标签设置在电池创作与indexpath的行
该getTableView功能是我自己
@IBAction func actionButtonPushed(_ sender: Any) {
guard let button = sender as? UIButton else { return }
guard let tableView = getTableView() else { return }
let indexPath = IndexPath(row: button.tag, section: 0)
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
if let delegate = tableView.delegate {
delegate.tableView!(tableView, didSelectRowAt: indexPath)
}
}