尝试创建自己的单元时出现问题 [英] issue when trying to create my own cell

查看:58
本文介绍了尝试创建自己的单元时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像下面这样自定义我的单元格

I'm working on customizing my cell like below

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *CellIdentifier = @"reusedCell";    
 DetailCell *cell = (DetailCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Customize the cell of each row from table
 if ( cell == nil ) {
     NSLog(@" MY CELL IS NIL");
     cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else
     NSLog(@" MY CELL IS NOT NIL");
}

通过调试,我知道根本没有到达4-5行

By debugging, I know that line 4-5 are not reached at all

MY CELL IS NOT NIL

因此,我无法创建自己的单元格.根据Apple文档

Therefore, I cant create my own cell. According to apple documentation

通常,数据源应该做的第一件事是将dequeueReusableCellWithIdentifier:发送到表视图,并传递一个重用标识符字符串.如果表视图未返回可重用的单元格对象,则数据源将创建一个,并在initWithStyle:reuseIdentifier:

Typically, the first thing the data source should do is send dequeueReusableCellWithIdentifier: to the table view, passing in a reuse-identifier string. If the table view does not return a reusable cell object, the data source creates one, assigning the object a reuse identifier in the final parameter of initWithStyle:reuseIdentifier:

因此,在我的情况下,表视图确实返回了可重用的单元格对象……它从何而来……以前有没有人遇到过此问题.请帮助谢谢.

Therefore, in my situation, the table view does return a reusable cell object... Where it comes from then... Have any one experienced this issue before. Please help thanks.

推荐答案

如果每次用户滚动表均值时都想创建新的单元格,则需要在自定义单元格类中重写UITableViewCell的复用标识符属性.

If you want to create new cells each time when the user scrolls the table means, you need to override the reuseIdentifier property of the UITableViewCell in your custom cell class.

- (NSString *) reuseIdentifier
{
    return @"";
}

这将使您可以创建新的单元格.但是,请检查内存使用情况.Bcoz,您每次用户滚动表格时都在创建新的单元格.

This will let you to create new cell. But, check out the memory usage. Bcoz you are creating new cell each time when the user scrolls the table.

这篇关于尝试创建自己的单元时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆