【问题标题】:iOS - Unable to scroll UITableView after remote content loadediOS - 加载远程内容后无法滚动 UITableView
【发布时间】:2013-01-07 21:33:16
【问题描述】:

当我通过一个块加载远程内容时,我通过调用 reloadData 来更新 UITableView。但是,我无法滚动查看这些数据。我认为这是因为我已经声明表中的行数是保存列表内容的 NSArray 变量的长度。但是,当调用它时,列表的计数为零。我会假设通过在 tableView 上调用 reloadData 它会再次重新计算列表大小。 也许我在这个过程中错过了一步。 谢谢 这是我的代码

 -(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"MYURL"]];
        [self performSelectorOnMainThread:@selector(fetchedData:)
                               withObject:data waitUntilDone:YES];

    });
}
-(void) fetchedData:(NSData *)responseData
{
    NSError* error;
    id json = [NSJSONSerialization
                          JSONObjectWithData:responseData //1
                          options:kNilOptions
                          error:&error];

    self.dataList = json;
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [self.tableView reloadData];
    });
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

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

    // Configure the cell...
    if([self.dataList count] > 0){
        NSDictionary *chartItem = [self.dataList objectAtIndex:indexPath.row];
        NSDictionary *song = [chartItem objectForKey:@"song"];
        cell.textLabel.text = [song objectForKey:@"title"];
    }

    return cell;
}

【问题讨论】:

  • 是否有足够的数据导致滚动。触摸时桌子会移动吗?前面是否有另一个视图(颜色清晰)正在拦截触摸调用?从您的代码中似乎没有什么明显的错误。表格是否显示数据但不允许您滚动?
  • 我认为 ivar 的发布是有原因的。 self.datalist 属性是如何定义的?随机错误 = 内存错误(经常)

标签: ios uitableview scroll


【解决方案1】:

检查您的json 对象(因此self.dataList)在异步调用结束时不为零。如果是,那么[self.dataList count] 将返回 0。

另外,请确保您正确设置了self.dataList.dataSource

【讨论】:

  • 感谢您的回复。不,JSON 对象不为零。当数据出现在表中时,我的 dataList 对象被正确填充。它只是不可滚动。这就是让我相信表格视图认为它有零行因此不启用滚动
  • 您是否在表中看到任何包含数据的行?
  • 是的,我可以看到大约 10 行半......其余的从屏幕上消失了。内容显示正确,只是无法滚动。
  • 在这种情况下,我会确保您的表格可以接受用户输入。检查dataList.userInteractionEnableddataList.scrollingEnabled 的值。另外,还可以查看dataList.contentSize的值,看内容高度是否大于表格高度。这是滚动的另一个必要条件。
【解决方案2】:

这正在工作。 问题的原因是我在 UITableView 中添加了平移手势

[self.view addGestureRecognizer:self.slidingViewController.panGesture];

这似乎干扰了在表格上滚动的能力。希望这对以后遇到此问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-28
    • 2011-07-21
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多