dequeueReusableCellWithIdentifier不返回我的自定义类型的单元格

scu*_*eve 5 iphone storyboard uitableview ios5

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

我在storyboard设计器中设置了我的单元标识符,只要我没有设置TDBadgedCell独有的任何属性,所有行都在UITableView中正确加载.如果我设置了badgeStringTDBadgedCell唯一的属性,我会得到一个例外.我缩小了dequeueReusableCellWithIdentifier:没有返回TDBadgedCell类型的单元格.

我只是用UITableViewController来运行它.我有一个UIViewController与嵌入式UITableView以相同的方式设置,这不是一个问题.有任何想法吗?

- (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");
Run Code Online (Sandbox Code Playgroud)

tim*_*imv 0

我有一个类似的问题,因为我正在子类化 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;    }
}
Run Code Online (Sandbox Code Playgroud)