【问题标题】:UILabel in detail view is not being populated未填充详细视图中的 UILabel
【发布时间】:2014-01-24 09:49:26
【问题描述】:

我有一个 UITableView,其中填充了从 SQLite 数据库收集的数据数组。选择单元格时,将与该单元格加载详细视图。导航控制器上的标题更改正常,但未填充详细视图上的 UILabel。

相关tableViewController代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    ExhibitorDetailViewController *exhibitorDetailViewControllerInstance = [[ExhibitorDetailViewController alloc] init];

    exhibitorDetailViewControllerInstance.theTitle = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    exhibitorDetailViewControllerInstance.exhibitorDetailModal = [arrayOfExhibitors objectAtIndex:indexPath.row];

    // Have tried setting the text before the detail view is loaded wiht the line below
    exhibitorDetailViewControllerInstance.LabelExhibitorDescription.text = arrayOfExhibitors.description;

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

}

详细视图控制器的viewDidLoad事件:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = _theTitle; //This sets the title ok

    NSLog(@"0 - %@", _exhibitorDetailModal.description); //This property is what I want to fill the UILabel with

    self.LabelExhibitorDescription.text = _exhibitorDetailModal.description; // This does nothing
    self.view.backgroundColor = [UIColor colorWithRed:0.6 green:0.8 blue:0.8 alpha:2.0f];  // If I don't set the background colour here it loads up black (despite being set in the storyboard)


    NSLog(@"label text = %@", self.LabelExhibitorDescription.text); // This shows the text box being null despite having its text property set above

}

详细视图头文件:

@interface ExhibitorDetailViewController : UIViewController
{

}
    @property (nonatomic, retain) NSString *theTitle;
    @property (strong, nonatomic) IBOutlet UILabel *nameLabel;
    @property (strong, nonatomic) IBOutlet UILabel *categoryLabel;
    @property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
    @property (strong ,nonatomic) NSArray *exhibitorDetailModal;
    @property (strong, nonatomic) IBOutlet UILabel *LabelExhibitorDescription;

@end

为什么没有填充 UILabel 文本?

谢谢。

编辑:

我已经记录了数组的“传出”描述 - [arrayOfExhibitors objectAtIndex:indexPath.row],这显示了预期的正确数据,与详细视图中显示的 _exhibitorDetailModal.description 相同。

【问题讨论】:

  • 如果您使用的是 iOS 7,请尝试上下滚动表格视图,看看标签是否被填充。我见过这种行为,但我不知道根本原因。
  • 是的,我正在使用 Xcode5 - iOS7。 tableView 看起来不错,我可以向上/向下滚动它并单击单元格加载详细视图 - 然后我可以点击 nag 控制器中的后退按钮并返回到 tableView 再次允许我单击其他单元格等等
  • 您是否尝试在您的didSelectRowAtIndexPath: 方法中记录[arrayOfExhibitors objectAtIndex:indexPath.row]
  • 是的,我已经检查过了,[arrayOfExhibitors objectAtIndex:indexPath.row] 的 NSLog 按预期给出了参展商的描述。我什至在详细视图 NSLog(@"0 - %@", _exhibitorDetailModal.description); 的 viewDidLoad 上记录了“接收属性”。这显示完全一样。
  • 看起来您正在使用主表视图中的数据填充标题。那不是MVC。您应该从模型(在本例中为数据库)获取数据。

标签: objective-c cocoa-touch uitableview uiviewcontroller


【解决方案1】:

为了最终让它工作,我重写了整个代码并一路整理。

为了让 UILabel 正确显示,我首先将 existorDetailModal 对象的 description 属性传递给 NSString。

_stringExhibitorDescription = _exhibitorDetailModal.description;    

然后我初始化了 UILabel。

self.labelExhibitorName = [[UILabel alloc]init];

然后将 UILabel 的文本设置为上面的 NSString 的文本

 [_labelExhibitorName setText:_stringExhibitorName];

然后将 UILabel 添加为 UIView 的子视图。

[self.view addSubview:_labelExhibitorDescription];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    相关资源
    最近更新 更多