ios - 调用 didSelectRowAtIndexPath 时 UIDetailView 不加载数据

标签 ios uitableview detailview

在我的 TableViewController 中,我设置了要加载到我的 DetailView (ws) 中的数据

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{

    detailViewController *detailVC = [[detailViewController alloc] init];
    detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
    [detailVC viewDidLoad] ;


    //detailVC.descriptionTextView = [[UITextView alloc] init];

    //[self.navigationController pushViewController:detailVC animated:YES];
}

这是我的 DetailView 必须加载的内容

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.barre.title = self.ws.associationName ;
    self.descriptionTextView.text = self.ws.associationDescription ;
}

但是当我选择该行时,会出现一个白页,而不是 TextView 中的 associationDescription,也不是我的 associationName 但是我的viewDidLoad好像是在detailVC.ws加载之前调用的,即我的detailVC是空的

这是我的 TableViewController.h

import UIKit/UIKit.h

@interface MasterViewController : UITableViewController{
    NSArray *tabAssociation;
}

@property (nonatomic, retain) NSArray *tabAssociation;


@end

还有 TableViewController.m

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {

}
@end

@implementation MasterViewController


@synthesize tabAssociation ;

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSArray *dictFromFile = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"AssociationTest" ofType:@"plist"]];    

    NSMutableArray *associationToAdd = [[NSMutableArray alloc] init];
    NSEnumerator *enumerator = [dictFromFile objectEnumerator];
    NSDictionary *anObject;
    while ((anObject = [enumerator nextObject])) {
    association *newAssocition = [[association alloc] initWithDictionaryFromPlist: anObject];
        [associationToAdd addObject: newAssocition];

    }

    self.tabAssociation = [NSArray arrayWithArray:associationToAdd];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tabAssociation.count;
}

- (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];
    }

    // On récupère l'objet Website qui correspon à la ligne que l'on souhaite afficher
    association *ws = [self.tabAssociation objectAtIndex:indexPath.row];

    cell.textLabel.text = ws.associationName;

  // On renvoie la cellule configurée pour l'affichage
    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{

detailViewController *detailVC = [[detailViewController alloc] init];
detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
//[detailVC viewDidLoad] ;
[self presentViewController:detailVC animated:YES completion:nil];


//detailVC.descriptionTextView = [[UITextView alloc] init];

//[self.navigationController pushViewController:detailVC animated:YES];
}

/*- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSDate *object = _objects[indexPath.row];
    [[segue destinationViewController] setDetailItem:object];
    }
}*/

@end

最佳答案

你不应该直接调用 -viewDidLoad。您要做的是以某种方式向您展示 detailViewController

如果你在 UINavigationController 中,像这样:

[self.navigationController pushViewController:detailVC animated:YES];

应该可以。如果不是,则必须考虑如何呈现 View Controller 。

你可以试试这样的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{

    detailViewController *detailVC = [[detailViewController alloc] init];
    detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
    [self presentViewController: detailVC animated:YES completion:nil];

}

关于ios - 调用 didSelectRowAtIndexPath 时 UIDetailView 不加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16883716/

相关文章:

swift - iOS 13 UISplitView 问题

ios - 如何在 CodingKeys 枚举(可编码协议(protocol))中使用通用值

ios - Firebase iOS - 使用复合键的一部分进行查询

ios - 在 Swift 中选择时关闭 UITableView Cell?

ios - 是否可以在 UItableView 中有多个部分,即 : Section 1 has two sections and these two sections have multiple sections

iphone - 带有 TabBar 导航控件和 TableView 方法的应用 didselectrowatindexpath

ios - 如何在Swift 3中使用持久化并检索与NSCoding兼容的对象到app文档目录?

ios - 如何在 RESTkit 0.2 中设置缓存策略

ios - 在 iOS 中关闭 View Controller 时 ContentInset 设置错误

yii2 - 在DetailView小部件中更改属性的值