cellForRowAtIndexPath 和 NSBundle mainBundle 的问题
有解决这个问题的想法吗?
这是正确和错误的方式:
这是工作代码:
- (UITableViewCell *)[...] cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@try {
newsRow = ((NewsRowController *)[tableView dequeueReusableCellWithIdentifier:@"cell"]);
if (newsRow == nil) {
if ( [[Device GetModel] isEqualToString:@"iPad Simulator"] ||
[[Device GetModel] isEqualToString:@"iPad"])
[[NSBundle mainBundle] loadNibNamed:@"NewsRow_ipad" owner:self options:nil];
else [[NSBundle mainBundle] loadNibNamed:@"NewsRow" owner:self options:nil];
if ([tableArray count] > 0)
[newsRow setData:[tableArray objectAtIndex:indexPath.row]];
}
}
@catch (NSException * e) {
NSLog(@"a!!!!!");
}
return newsRow;
}
...但是因为在我的 3g 设备中,它缓慢当我向上/向下滚动表格时,我以这种方式更改了代码,插入设备检查在 viewDidLoad 中并将其传递到 cellForRowAtIndexPath 中:
NSArray *cRow;
@property (nonatomic, retain) NSArray *cRow;
[...]
@syntetize cRow;
- (void)viewDidLoad {
[...]
if ( [[Device GetModel] isEqualToString:@"iPad Simulator"]
||
[[Device GetModel] isEqualToString:@"iPad"])
cRow = [[[NSBundle mainBundle] loadNibNamed:@"NewsRow_ipad" owner:self options:nil] retain];
else cRow = [[[NSBundle mainBundle] loadNibNamed:@"NewsRow" owner:self options:nil] retain];
[...]
}
- (UITableViewCell *)[...] cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@try {
newsRow = ((NewsRowController *)[tableView dequeueReusableCellWithIdentifier:@"cell"]);
if (newsRow == nil) {
//
// ADDED CODE.
//
newsRow = [cRow objectAtIndex:0];
//
if ([tableArray count] > 0)
[newsRow setData:[tableArray objectAtIndex:indexPath.row]];
}
}
@catch (NSException * e) {
NSLog(@"a!!!!!");
}
return newsRow;
}
现在表格只显示一行。如果我滚动,行会发生变化,但它只显示一行...
有什么问题吗?
有什么帮助吗?
谢谢,
一个
have an idea to solve this problem?
This is the correct and wrong way respectly:
This is working code:
- (UITableViewCell *)[...] cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@try {
newsRow = ((NewsRowController *)[tableView dequeueReusableCellWithIdentifier:@"cell"]);
if (newsRow == nil) {
if ( [[Device GetModel] isEqualToString:@"iPad Simulator"] ||
[[Device GetModel] isEqualToString:@"iPad"])
[[NSBundle mainBundle] loadNibNamed:@"NewsRow_ipad" owner:self options:nil];
else [[NSBundle mainBundle] loadNibNamed:@"NewsRow" owner:self options:nil];
if ([tableArray count] > 0)
[newsRow setData:[tableArray objectAtIndex:indexPath.row]];
}
}
@catch (NSException * e) {
NSLog(@"a!!!!!");
}
return newsRow;
}
...but because in my 3g device it's slowly when i scroll table up/down i changed the code in this way, inserting Device check in viewDidLoad and pass it in cellForRowAtIndexPath
:
NSArray *cRow;
@property (nonatomic, retain) NSArray *cRow;
[...]
@syntetize cRow;
- (void)viewDidLoad {
[...]
if ( [[Device GetModel] isEqualToString:@"iPad Simulator"]
||
[[Device GetModel] isEqualToString:@"iPad"])
cRow = [[[NSBundle mainBundle] loadNibNamed:@"NewsRow_ipad" owner:self options:nil] retain];
else cRow = [[[NSBundle mainBundle] loadNibNamed:@"NewsRow" owner:self options:nil] retain];
[...]
}
- (UITableViewCell *)[...] cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@try {
newsRow = ((NewsRowController *)[tableView dequeueReusableCellWithIdentifier:@"cell"]);
if (newsRow == nil) {
//
// ADDED CODE.
//
newsRow = [cRow objectAtIndex:0];
//
if ([tableArray count] > 0)
[newsRow setData:[tableArray objectAtIndex:indexPath.row]];
}
}
@catch (NSException * e) {
NSLog(@"a!!!!!");
}
return newsRow;
}
Now the table show me only a row. If i scroll, the row change, but it show only one row...
What's the problem?
Any helps?
thanks,
A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,当您调用 dequeueReusableCellWithIdentifier: 时,您将返回 nil,因为没有可以出列的单元。每次返回零单元格时都需要加载笔尖,否则您将不会在第一个单元格之后向表中添加任何单元格。
如果调用 GetModel 导致速度变慢,您可以在 viewDidLoad 中调用它并将其存储以供以后使用。
由于您在每个单元格中绘制的视图数量,您的表格很可能无法平滑滚动。如果您的背景始终是纯色,您应该确保添加到单元格的所有视图都是不透明的。如果您需要图像或渐变背景,那么您应该考虑在一个视图中绘制所有单元格。 Apple 在此处提供了如何执行此操作的一个很好的示例: TableViewSuite。具体检查 TimeZoneView 文件。
In this case when you call dequeueReusableCellWithIdentifier: you will get nil back because there are no cells that can be dequeued. You need to load the nib each time you get a nil cell back otherwise you won't be adding any cells to the table after the first one.
If calling GetModel is what is slowing you could call that in viewDidLoad and store it for usage later on.
More than likely your table is not scrolling smoothly because of the amount of views you are drawing in each cell. If your background will always be a solid color you should make sure that all the views you are adding to the cell or opaque. If you need an image or a gradient background then you should look at drawing the cell all in one view. Apple has a good example of how to do this here: TableViewSuite. Specifically checkout the TimeZoneView file.