【问题标题】:UITableView datasource methods not called on reloadData in IOSUITableView 数据源方法未在 IOS 中的 reloadData 上调用
【发布时间】:2013-07-09 11:30:09
【问题描述】:

我被这个问题困扰了好几个小时,还没有找到任何解决方案……

我在UIViewController 中有一个表格作为子视图。加载视图时,为tableview 调用所有数据源方法,但是当我从服务器接收数据并调用[someTable reloadData] 时,没有调用任何数据源方法。我已经通过断点确认我的数组确实包含 100 个对象。代理和数据源都绑定到 IB 中的File owner

可能缺少什么?请帮助我...谢谢:(

OutLogController.h

@interface OutLogController : UIViewController<UITableViewDataSource,UITableViewDelegate,ASIHTTPRequestDelegate>
{
    NSMutableArray *outstandingKeys;

}
@property (strong, nonatomic) IBOutlet UITableView *someTable;

@end

OutLogController.m

@interface OutLogController ()

@end

@implementation OutLogController

@synthesize someTable;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    outstandingKeys = [[NSMutableArray alloc] init];
    someTable = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    someTable.rowHeight = 85;
    [self retrieveOutstandingLogFromServer];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return outstandingKeys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    // Configure the cell...
    LogUnit *unit = [outstandingKeys objectAtIndex:indexPath.row];

     cell.textLabel.text = unit.symbol;

    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

-(void) requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@",request.responseString);
    NSArray *list = [request.responseString JSONValue];//contains 100 objects

    for (int i = 0; i < list.count; i++) {

        NSArray *singleTrade = [[list objectAtIndex:i] JSONValue];

        LogUnit *unit = [[LogUnit alloc] init];

        unit.symbol = [singleTrade objectAtIndex:0];
        unit.type = [singleTrade objectAtIndex:1];
        unit.price = [singleTrade objectAtIndex:2];
        unit.remaining = [singleTrade objectAtIndex:3];
        unit.order = [singleTrade objectAtIndex:4];
        unit.time = [singleTrade objectAtIndex:5];

        [outstandingKeys addObject:unit];

    }

    if(outstandingKeys.count > 0)
    {
        [someTable reloadData];//does reload table

    }

}

编辑

删除线后:

someTable = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

Datasource 方法被调用,cellForRowAtIndexPath 除外,并且表格从视图中消失,因为没有调用此方法。知道为什么它不能被调用吗?

注意:我已经确认在服务器响应后我的 count = 100。

【问题讨论】:

  • someTable 是一个 IBoutlet,所以你应该将它连接到 XIB 中的表是否已连接它
  • someTable = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
  • 你可能已经解决了这个问题,但是这个答案帮助我解决了这个问题:stackoverflow.com/questions/4163579/…

标签: ios datasource tableview reload


【解决方案1】:
someTable = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

您已经从 nib/storyboard 绑定了 UITableView。在这种情况下,您不必再次分配 tableview。删除此行即可完成工作

【讨论】:

  • 我删除了这一行,除了cellForRowAtIndexPath之外的所有数据源方法都被调用了。
  • 检查你返回的行数是否不为零。
  • 返回的计数是 100。我不知道为什么在重新加载时,表已经完全消失了!
  • OutstandingLogUnit 和 LogUnit 相同或不同,因为您插入对象的“OutstandingLogUnit”并获取对象的 LogUnit 类型。
  • 对不起……这是我的编辑错误……我已经更正了
【解决方案2】:

someTable = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

您创建了新的 tableView,但它不会添加到当前视图中。

【讨论】:

  • 是的,但是 [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain] 创建 +1
【解决方案3】:

检查您的outstandingKeys 计数可能为零计数

【讨论】:

  • 你检查委托方法是否连接
【解决方案4】:

尝试通过在主线程上执行来重新加载数据..

dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableRating reloadData];
    });

这将解决您的问题。快乐编码..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2010-12-17
    • 2016-01-17
    • 1970-01-01
    • 2017-02-11
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多