【问题标题】:dequeueReusableCellWithIdentifier not returning cell of my custom typedequeueReusableCellWithIdentifier 不返回我的自定义类型的单元格
【发布时间】:2012-03-23 13:31:04
【问题描述】:

我正在使用带有 UITableViewController 和 UITableViewCell 子类的 ios5 故事板。我不想在情节提要设计器中为视图设计单元格的视觉元素,因为我想使用 UITableViewCell 的可重用子类(特别是TDBadgedCell)。

我已经在故事板设计器中设置了我的单元格标识符,并且只要我没有设置任何 TDBadgedCell 独有的属性,所有行都会正确加载到 UITableView 中。如果我设置了 TDBadgedCell 独有的 badgeString 属性,则会出现异常。我缩小范围,dequeueReusableCellWithIdentifier: 没有返回 TDBadgedCell 类型的单元格。

我只是在使用 UITableViewController 时遇到了这个问题。我有一个带有嵌入式 UITableView 的 UIViewController 以相同的方式设置,这不是问题。有什么想法吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"PhoneNumberCell";
TDBadgedCell *cell = (TDBadgedCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if ([cell isKindOfClass:[TDBadgedCell class]])
{
    NSLog(@"It is TDBadgedCell");
}
else
    NSLog(@"It is NOT TDBadgedCell");

【问题讨论】:

  • 你是否也在故事板设计器中将“自定义类”更改为 TDBadgedCell?
  • Prototype Cells in a nib instead of a storyboard 的可能重复项 - 唯一可能,我不确定您是否有此单元格的单独 xib。
  • 6 月 1 日 - 就是这样!谢谢!不过,我不确定如何将您的答案标记为答案。

标签: iphone ios5 uitableview storyboard


【解决方案1】:

我有一个类似的问题,我正在继承 UITableViewCell 但不使用情节提要。这是我使用不同单元格类的解决方案,具体取决于用户是否购买了应用程序的解锁功能。希望它可以帮助某人。

简而言之,我的单元格包含多个对象,包括 UITextView 对象。我想在精简版中锁定 UITextView 对象的复制和粘贴功能,然后在用户购买应用内产品后发布该功能。

我有两个 UITableViewCell 类,一个是 UITextView,另一个是 UITextView 的子类,canBecomeFirstresponder 返回 NO。这样用户仍然可以上下滚动 UITextview 数据,但不能复制和粘贴数据。

这是代码,我所要做的就是重命名重用标识符。

为什么?因为 [self.tableview reloadData] 不会用新类重建单元格,因为单元格仍然存在。屏幕上的新单元格将获得新类,但现有单元格不会。此解决方案在购买解锁添加的功能后立即重建所有单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


if (your test if in-app was purchased is yes)
{
static NSString *MyIdentifier = @"MyCell";

FrontCell *cell = (FrontCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil)
        {
        cell = [[FrontCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.shouldIndentWhileEditing = NO;
    }

//....///     

cell.trackDetails.text = [yourObject objectAtIndex:indexPath.row];
cell.trackDetails.delegate = self;
cell.trackDetails.tag = indexPath.row;


return cell;
}
else // inapp not purchased
{
    static NSString *MyLockedIdentifier = @"MyLockedCell";

    FrontCellLocked *cell = (FrontCellLocked *)[tableView dequeueReusableCellWithIdentifier:MyLockedIdentifier];

    if (cell == nil)
    {
        cell = [[FrontCellLocked alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyLockedIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.shouldIndentWhileEditing = NO;
    }

     //....///     

cell.trackDetails.text = [yourObject objectAtIndex:indexPath.row];
cell.trackDetails.delegate = self;
cell.trackDetails.tag = indexPath.row;


return cell;    }
}

【讨论】:

    【解决方案2】:

    在故事板中,您可以为 UITablviewCell 的子类设置自定义类属性。
    然后 dequeueReusableCellWithIdentifier 方法将返回具有您的子类类型的单元格。

    【讨论】:

      【解决方案3】:

      我认为您使用了错误的方法来使单元格出队。

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
          UITableViewCell *cell = [self.tblProfileInfo dequeueReusableCellWithIdentifier:@"PostCell" forIndexPath:indexPath];
          cell.selectionStyle = UITableViewCellSelectionStyleNone;
          return cell;
      
      }
      

      最后你忘记了 indexPath。

      【讨论】:

      • 尝试更改“TDBadgedCell cell = (TDBadgedCell)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];”到“TDBadgedCell cell = (TDBadgedCell)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];”
      猜你喜欢
      • 2016-06-07
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 2012-04-17
      • 1970-01-01
      • 2013-01-05
      相关资源
      最近更新 更多