gpt4 book ai didi

objective-c - TableView Controller 如何与其详细 View 通信?

转载 作者:行者123 更新时间:2023-11-28 23:08:00 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com