【问题标题】:UITableView not loading cell data when scrolling滚动时UITableView不加载单元格数据
【发布时间】:2014-04-28 13:44:55
【问题描述】:

我在我的视图控制器上使用 1 个原型单元设置了 UITableView。当视图控制器加载时,它会查询我的数据库(出于安全原因删除代码)使用 CustomisationObject 创建一个新对象,然后将其存储到一个数组中。

当我运行应用程序时,加载的 4 个单元格具有存储在 localArray 中的正确值。

当我向下滚动查看剩余的两个单元格时,它们尚未加载,值为空白。

自定义单元格类

CustomisationCell.h

#import <UIKit/UIKit.h>

@interface CustomisationCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *titleOutlet;
@property (weak, nonatomic) IBOutlet UISlider *sliderOutlet;

@end

CustomisationCell.m

#import "CustomisationCell.h"

@implementation CustomisationCell
@synthesize titleOutlet, sliderOutlet;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

对象类

CustomisationObject.h

@interface CustomisationObject : NSObject

@property (nonatomic, weak) NSString *localChannel;
@property (nonatomic, weak) NSString *localValue;

@end

查看控制器类

CustomisationViewController.h

#import <UIKit/UIKit.h>

@interface CustomisationViewController : UIViewController <UITableViewDelegate>

@end

CustomisationViewController.m

#import "CustomisationViewController.h"
#import "CustomisationCell.h"
#import "CustomisationObject.h"

@interface CustomisationViewController ()

@end

@implementation CustomisationViewController
NSMutableArray *localArray;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // SQL Code removed - query is return into variable resultSet

    localArray = [[NSMutableArray alloc] init];

    while([resultSet next])
    {   
        CustomisationObject *customisationObject = [[CustomisationObject alloc] init];
        customisationObject.localChannel = [resultSet stringForColumn:@"COLUMN_ONE"];
        customisationObject.localValue = [resultSet stringForColumn:@"COLUMN_TWO"];
        [localArray addObject:customisationObject];
    }
    [database close];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [localArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomisationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.titleOutlet.text = [[localArray objectAtIndex:[indexPath row]] localChannel];
    cell.sliderOutlet.value = [[[localArray objectAtIndex:[indexPath row]] localValue] intValue];

    return cell;
}

@end

如果我更改表格视图的高度以使所有 6 个单元格都适合(无需滚动),它们都会按预期加载。它似乎只在细胞进出游戏时出现。

【问题讨论】:

  • 你有没有在cellForRowAtIndexPath 中添加断点来看看这个小流氓在做什么?我怀疑你会发现dequeueReusableCellWithIdentifier 正在返回nil,这很正常。
  • 还要确保 localArray 包含您希望它包含的所有对象 - 再次断点并查看该对象的内容。
  • 单元格的打印描述:>
  • Cell 似乎没有返回 nil。我在 viewDidLoad 中遍历了 localArray,所有对象都在那里。
  • 如果我更改表格视图的高度以使所有 6 个单元格都适合(无需滚动),它们都会按预期加载。它似乎只在细胞进出游戏时出现。

标签: ios objective-c uitableview nsmutablearray


【解决方案1】:

全部排序。事实证明,CustomisationObject.h 中的内存属性需要复制而不是弱。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多