我有一个 TableView ,如果点击某一行,就会显示详细 View 。但是详细 View 如何知道哪一行被点击了? 因此,例如,我有一个显示名称的 MainController。如果我点击“Johnny”,下一个屏幕应该会显示一个带有字符串“Johnny”的标签。我该怎么做?
[编辑 - 添加代码]
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ArticleView *article = [self.storyboard instantiateViewControllerWithIdentifier:@"ArticleView"];
[article.myLabel setText:@"random"];
[self.navigationController pushViewController:article animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.textLabel.text = [info objectForKey:@"username"];
cell.textLabel.backgroundColor = nil;
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.backgroundColor = nil;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.text = [info objectForKey:@"user_pic"];
// Configure the cell...
return cell;
}
ArticleView.h
@interface ArticleView : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
ArticleView.m
->综合属性
最佳答案
您可以将对象(即字符串 Jonny)作为属性传递给详细 View Controller 。
@interface ArticleViewController : UIViewController
@property (retain) ArticleView *articleView;
@end
//Don't forget to synthesize name
在 TableView Controller 中
-(void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath)indexPath
{
NSDictionary *info = [json objectAtIndex:indexPath.row];
ArticleViewController *articleViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ArticleViewController"];
articleViewController.articleView = articleView;
[self.navigationController pushViewController: articleViewController animated:YES];
}
关于objective-c - TableView Controller 如何与其详细 View 通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8943343/