繁体   English   中英

UILabel在cellForRowAtIndexPath中为nil

[英]UILabel is nil in cellForRowAtIndexPath

我正在将Storyboard的原型单元与自定义表格单元类一起使用,而UILabel在cellForRowAtIndexPath中为零。 Xcode的标识符正确,已初始化单元格,并且已初始化默认的UILabel(即textLabel和detailTextLabel),但未初始化我添加的自定义UILabel并为其设置了IBOutlet。 我尝试过的几件事:

  • 我尝试删除ViewDidLoad中的registerClass调用,但是由于单元格需要将其初始化为自定义类GenericDetailCell而崩溃。

  • viewWithTag返回nil

  • 我尝试遍历UITableView的所有子视图,以查看是否获取了错误的单元格。 正确的单元格由dequeueReusableCellWithIdentifier返回

有人碰到这个吗?

@implementation PeopleGroupPickerViewController
{
    NSArray *people;
    NSArray *searchResults;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    people = [[DAL sharedInstance] getPeople:false];
    [self.tableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:@"PersonCell"];
    [self.searchDisplayController.searchResultsTableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:@"PersonCell"];

    // stackoverflow.com/questions/5474529
    [self.searchDisplayController setActive:YES];
    [self.searchDisplayController.searchBar becomeFirstResponder];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        return 1;
    }
    else
    {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        return searchResults.count;
    }
    else
    {
        return people.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GenericDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];

    Person_ *thisPerson;
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
        thisPerson = (Person_ *) searchResults[indexPath.row];
        NSLog(@"searchResultsTableView, %@",thisPerson.sName);
    }
    else
    {
        thisPerson = (Person_ *) people[indexPath.row];
        NSLog(@"UITableView, %@",thisPerson.sName);
    }
    Person_ *thisSpouse = [[DAL sharedInstance] getSpouse:thisPerson People:people];

    // cell.fieldName and cell.fieldValue are nil, cell is not nil
    cell.fieldName.text = thisPerson.sName;
    cell.fieldValue.text = thisSpouse.sName;

    return cell;
}

GenericDetailCell.h:

@interface GenericDetailCell : UITableViewCell

@property (nonatomic,weak) IBOutlet UILabel *fieldName;
@property (nonatomic,weak) IBOutlet UILabel *fieldValue;

@end

在我看来,您可能正在尝试将UITableViewCell预定义样式(例如UITableViewCellStyleSubtitle )与自定义单元格合并。

在情节提要中,如果您为原型单元格选择了预定义的样式,则其他控件将不会被识别。

要解决此问题,请在Prototype UITableViewCell的属性检查器中,选择“自定义”类型。 然后将所需的所有控件添加到“原型”单元中,包括替换以前自动添加到预定义单元格类型的默认控件所需的那些控件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM