我的自定义表格视图单元格下面有以下代码段。我在每个自定义单元格中有5个标签,这些标签可以通过标签而不是标签来区分。 (每个标签都有不同的标签)。没有使用标签ID。因此,如果我通过下面显示的方法从单元格中的标签中提取NSString,我怎么知道正在拉出哪个字符串? 我不确定这段代码是否可行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NoViewController *noViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NoViewController"];
NSString *label1 = [_nodes objectAtIndex:indexPath.row];
NSString *label2 = [_nodes objectAtIndex:2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Node *node = [self.nodes objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"NodeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CellIdentifier forIndexPath:indexPath];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = node.nodeid;
label = (UILabel *)[cell viewWithTag:2];
label.text = [NSString stringWithFormat:@" %.0f"];
label = (UILabel *)[cell viewWithTag:3];
label.text = [NSString stringWithFormat:@" %.0f"];
label = (UILabel *)[cell viewWithTag:4];
label.text = [NSString stringWithFormat:@" %.0f"];
label = (UILabel *)[cell viewWithTag:5];
label.text = [NSString stringWithFormat:@" %.0f"];
label = (UILabel *)[cell viewWithTag:6];
label.text = [NSString stringWithFormat:@" %.0f"];
label = (UILabel *)[cell viewWithTag:7];
label.text = [NSString stringWithFormat:@" %.0f"];
return cell;
}
AppDelegate.m
RKManagedObjectMapping *nodeMapping = [RKManagedObjectMapping mappingForClass:[Node class] inManagedObjectStore:objectStore];
答案 0 :(得分:0)
您需要做的就是执行一个segue,然后在prepareForSegue
中分配所选节点:
<强> didSelectRowAtIndexPath方法强>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"detailSegue" sender:self];
}
<强> prepareForSegue 强>
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"detailSegue"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NoViewController *noViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NoViewController"];
noViewController.selectedNode = [_nodes objectAtIndex:indexPath.row];
}
}
<强> DetailViewController.h 强>
#import "Node.h"
@interface DetailViewController : UIViewController
@property (nonatomic, strong) Node *selectedNode;//or whatever type
@end
<强> DetailViewController.m 强>
-(void)viewDidLoad{
mylabel1.text = _selectedNode.lastdelivered1;
mylabel2.text = _selectedNode.lastdelivered2;
//and so forth
}
答案 1 :(得分:0)
虽然您当前的代码很不清楚它打算实现什么,但似乎您正在尝试提取一些标签字符串,并将其传递给另一个视图控制器。
以下是所涉及步骤的高级概述:
1 - 您必须首先获得您点击的单元格的引用。通常,拨打cellForRowAtIndexPath
应该会有所帮助。
MyTableViewCell * cell = [myTableView cellForRowAtIndexPath:indexPath];
2 - 从单元格中获取标签参考:
UILabel * label = [cell viewWithTag:TAG_ASSIGNED]; //TAG_ASSIGNED should be assigned under cellForRowAtIndexPath earlier.
请注意,如果您的单元格有自定义类,并且已为UILabel指定了出口,还有其他方法可以从单元格获取标签 - 这是非常常见的设计,但这里的答案范围不够。
3 - 获取标签文本:
NSString * text = label.text;
4 - 实例化视图控制器 - 只执行一次而不是每次点击。要维护引用,请为其分配iVar。
喜欢.h文件:
NoViewController * noViewController;
在.m文件中,didSelectRowAtIndexPath
下:
if (!noViewController)
{
noViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NoViewController"];
}
总而言之,您的didSelectRowForIndexPath应如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell * cell = [myTableView cellForRowAtIndexPath:indexPath];
UILabel * label = [cell viewWithTag:TAG_ASSIGNED]; //TAG_ASSIGNED should be assigned under cellForRowAtIndexPath earlier.
NSString * text = label.text;
if (!noViewController)
{
noViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NoViewController"];
} //Do whatever now..
}
如果这就是你想要的,那就应该这样做!