从第2行(或不在第一行)开始重新加载uitableview

时间:2013-05-02 02:04:42

标签: cocoa-touch uitableview reloaddata

再次出现Uitableview问题。每当我重新加载我的数据时,tableview会重新加载,但似乎数据不会从第一行开始。请参考下图。

重新加载数据后我尝试了以下操作,但仍未成功:

[self.tableView reloadData];
// this or the other one ... [self.tableView setContentOffset:CGPointZero animated:NO];
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

这就是我在viewdidload上定位uitableview的方法

self.tableView.frame = CGRectMake(450, 20, self.tableView.frame.size.width, self.tableView.frame.size.height - 20);

编辑1:

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

    User *user = [users objectAtIndex:indexPath.row];
    UserTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.name.text =  [NSString stringWithFormat:@"%@ %@", user.firstName, user.lastName];

    return cell;
}

1 个答案:

答案 0 :(得分:1)

根据您的评论,当您将设备旋转到横向模式时,表格的大小调整不正确。在故事板中,您需要在表视图上设置自动调整大小,以便正确调整所需的大小。

这是一个很好的起点:

enter image description here

注意在Autosizing标签上方,它显示了一堆红色条(称为弹簧和支柱)。我将它们全部打开,这将保持与超视图边缘的距离,并允许对象改变其大小以保持这些距离相同。这应该接近你想要的,如果没有,那么你可以使用不同的组合,直到你得到你想要的。

点击红线即可打开和关闭它的每个部分。

有关详细信息,请查看以下问题:Autoresizing masks programmatically vs Interface Builder / xib / nib