我的表视图中有两个自定义单元格,可以正确显示。 MyCustomCellB
有一个按钮,当用户点击此按钮时,我以编程方式调用segue,这也可以,但导入错误的变量。它需要“属于”MyCustomCellA
类型单元格的不同数据。但是,如果我首先选择单元格,然后点击按钮它工作正常,在选择后它会得到正确的数据。
我的cellForRowAtIndexPath:
不正确吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([object[@"num"] isEqualToString:@"one"]) {
MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//... cell display
cell.acceptLabel.tag = indexPath.row;
[cell.acceptLabel addTarget:self action:@selector(didTapBttn:) forControlEvents:UIControlEventTouchUpInside];
return cell;
} else {
MyCustomTableViewCellB *cellMessage = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
//... cell display
return cellMessage;
}
}
- (void)didTapBttn:(id)sender {
[self performSegueWithIdentifier:@"info" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"info"]) {
if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DViewController *datap = [segue destinationViewController];
PFObject *passObj = [self.objects objectAtIndex:indexPath.row];
datap.infoName = passObj[@"infoName"];
datap.infoId = passObj[@"id"];
}
if ([[segue identifier] isEqualToString:@"desc"]) {
if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DViewController *datap = [segue destinationViewController];
PFObject *passObj = [self.objects objectAtIndex:indexPath.row];
datap.infoN = passObj[@"descName"];
datap.infoId = passObj[@"descId"];
}
}
答案 0 :(得分:1)
您可以使用某些数据(即NSDictionary)扩展didTappBttn,因此您必须确保它始终包含所需的数据。如果您不解释它,代码中的标记也不相关。也许数据应该是indexpath标记处的行。
答案 1 :(得分:1)
首先,由于除了调用performSegue之外,你没有在你的按钮方法中做任何事情,你应该直接从按钮而不是控制器创建segue,并删除按钮的动作方法。然后在prepareForSegue:sender:中,sender参数将是按钮。您需要获取包含按钮的单元格的indexPath。有多种方法可以执行此操作。一种方法是使用表视图方法indexPathForRowAtPoint:,并将按钮的原点(转换为表视图的坐标系后)作为点传递。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender {
if ([[segue identifier] isEqualToString:@"info"]) {
if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {
CGPoint p = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
DViewController *datap = [segue destinationViewController];
PFObject *passObj = [self.objects objectAtIndex:indexPath.row];
datap.infoName = passObj[@"infoName"];
datap.infoId = passObj[@"id"];
}
if ([[segue identifier] isEqualToString:@"desc"]) {
if ([[segue destinationViewController] isKindOfClass:[DViewController class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DViewController *datap = [segue destinationViewController];
PFObject *passObj = [self.objects objectAtIndex:indexPath.row];
datap.infoN = passObj[@"descName"];
datap.infoId = passObj[@"descId"];
}
}
我不知道您是否需要对“desc”segue进行相同的更改,因为我不知道该触发器是如何被触发的。