【问题标题】:UILabel inside cell in UITableView is always null (two cell prototypes)UITableView 中单元格内的 UILabel 始终为空(两个单元格原型)
【发布时间】:2013-03-07 09:57:20
【问题描述】:

我正在尝试在表格视图中填充单元格(我有两种自定义类型的单元格,它们在情节提要中创建了不同的元素,标识符为“info_cell”和“person_cell”,在 UITableView 上方的分段控件上我决定加载什么 [tableView重新加载])。当我尝试访问单元格内的 UILabels 时,我发现标签为空。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = (viewType == INFO_VIEW) ? @"info_cell" :@"person_cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if(viewType == INFO_VIEW){
        NSLog(@"INFO = %@", info_text_some_string);
        UILabel *lblInfo = (UILabel *)[cell viewWithTag:200];
        [lblInfo setText:info_text_some_string];
    }
    else{
        // there is part for person
    }
    return cell;
}

当我在表格中只有一个原型单元格(UITableView 在 UIVewController 中)时,相同的代码可以工作。这里有什么问题,我检查了 100 次:单元格标识符是 OK,标签标签是 200。

这是 UISegmentControl 的操作

- (IBAction)changeView:(id)sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

    if (selectedSegment == 0) {
        viewType = INFO_VIEW;
    }
    else{
        viewType = PERSON_VIEW;
    }
    [tableView reloadData];
}

我为 tableView 添加了必要的方法并连接委托 i 数据源。 有谁知道为什么它为空?

【问题讨论】:

  • 你能发布对你有用的解决方案吗,因为我面临着类似的问题..

标签: ios ios5 ios6


【解决方案1】:

尝试这个通常我在 CellRorRowAtIndexPath

中使用自定义单元格时遵循此过程
   if(!cell)
    {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        //customcell is your UITableViewCell created by you in ur xib
        NSData *archivedData =[NSKeyedArchiver archivedDataWithRootObject:customcell];
        cell =[NSKeyedUnarchiver unarchiveObjectWithData:archivedData];

    }
    if(viewType ==INFO_VIEW)
     {
    [(UILabel *)[cell viewWithTag:200] setText:@"you text"];
     }
  else{
        // person view....
      }

这样您就可以收集单元格的所有元素并为其设置值。请分享您的结果

【讨论】:

    【解决方案2】:

    假设您正确地对 UITableViewCells 进行了子类化(例如,我使用 InfoCell 和 PersonCell),您可以试试这个:

    if(viewType == INFO_VIEW)
    {
        InfoCell *cell = (InfoCell *)[tableView dequeueReusableCellWithIdentifier:@"info_cell"];
        // do your stuff for info here
    }
    else if(viewType == PERSON_VIEW)
    {
        PersonCell *cell = (PersonCell *)[tableView dequeueReusableCellWithIdentifier:@"person_cell"];
        // do your stuff for person here
    }
    

    【讨论】:

      【解决方案3】:

      为什么不做这样的事情呢?

      [[cell textLabel] setText: @"text goes here"];
      

      跳过 UILabel 部分?

      【讨论】:

      • 我现在添加了,它是自定义类型的单元格,上面有不同的元素
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多