我的表视图控制器中有六个标签,它们都具有相同的ID,但是它们有不同的标签。单击一个单元格后,我想将每个标签的值(浮点值)传递给下一个视图控制器活动。我不知道如何解决这个问题,以及在.h文件和.m文件中声明什么以使其工作。以下是.m tableviewcontroller
文件中的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NodeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CellIdentifier forIndexPath:indexPath];
UILabel *label;
Node *node = [self.nodes objectAtIndex:indexPath.row];
label = (UILabel *)[cell viewWithTag:1];
label.text = node.nodeid;
label = (UILabel *)[cell viewWithTag:2];
label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered1.floatValue];
label = (UILabel *)[cell viewWithTag:3];
label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered2.floatValue];
label = (UILabel *)[cell viewWithTag:4];
label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered3.floatValue];
label = (UILabel *)[cell viewWithTag:5];
label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered4.floatValue];
label = (UILabel *)[cell viewWithTag:6];
label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered5.floatValue];
label = (UILabel *)[cell viewWithTag:7];
label.text = [NSString stringWithFormat:@" %.0f", node.lastdelivered6.floatValue];
return cell;
}
答案 0 :(得分:0)
:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Controller *myController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"yourViewControllerIdentifer"];
myController.node = [self.nodes objectAtIndex:indexPath.row];
[self presentViewController:myController animated:YES completion:Nil];
//or [self.navigationController pushViewController:myController animated:YES]; if Navigation Controller use
}
在.h文件中接收节点
的控制器@interface Controller : UIViewController
@property (strong, nonatomic) Node *node;
@end
答案 1 :(得分:0)
尝试将所有标签文本保存到NSArray *labelsArr
,然后在下一个ViewController的标题中添加新属性
@property (nonatomic, strong) NSArray *savedArr;
然后你在didSelectRowAtIndexPath中传递你到其他的
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Controller *myController = [[Controller alloc] init];
myController.savedArr = labelsArr;
// here call you VC ass you want;
}
但如果您使用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
我建议你在那里做myController.savedArr = labelsArr;
。
不要忘记在viewDidLoad
或viewWillAppear
中将标签文本从数组放到您想要的位置。
这样你可以将文本传递给其他标签,但对我来说使用数组更好。