cellForRowAtIndexPath 和 NSBundle mainBundle 的问题

发布于 2024-10-13 12:04:36 字数 2297 浏览 6 评论 0原文

有解决这个问题的想法吗?

这是正确和错误的方式
替代文本 替代文本

这是工作代码:

- (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:
alt text alt text

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...
alt text
What's the problem?

Any helps?

thanks,
A

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋心╮凉 2024-10-20 12:04:36

在这种情况下,当您调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文