【问题标题】:Restarting UITableView issue重启 UITableView 问题
【发布时间】:2012-06-24 21:14:32
【问题描述】:

我有一个 UITableView 不是在情节提要中创建的,而是通过代码创建的。可以通过选择其他一些冠军来刷新此表,但是当我这样做时,会有另一个包含旧数据的表...我在刷新时设置为零...

有人有想法吗??

要填写表格,我使用此代码

listVideo = [listGroupes objectAtIndex:indexPath.row];
montageListView= [[UITableView alloc] initWithFrame:listFrame style:UITableViewStylePlain];
montageListView.delegate = self;
montageListView.dataSource = self;
montageListView.layer.cornerRadius = 10;
[listMontagesView addSubview:montageListView];

所以有一个带有其他选项的新视图,但如果我返回此代码:

montagesListView removeFromSuperview];
montagesListView = nil;

我用相同的代码重新填充它

listVideo = [listGroupes objectAtIndex:indexPath.row];
montageListView= [[UITableView alloc] initWithFrame:listFrame style:UITableViewStylePlain];
montageListView.delegate = self;
montageListView.dataSource = self;
montageListView.layer.cornerRadius = 10;
[listMontagesView addSubview:montageListView];

还有一张好表,但还有一张旧数据…… 我真的没有发现代码有问题:/

这是行单元格的代码,但我认为这不是问题,

    static NSString *MyIdentifier = @"MyIdentifier";
NSString *text;
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

// If no cell is available, create a new one using the given identifier.
if (cell == nil) {
    // Use the default cell style.
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MyIdentifier];
}

if (tableView == groupListView) {
    text = [listGroupesNames objectAtIndex:indexPath.row];
}else {

    text = [[listVideo objectAtIndex:indexPath.row]description];
    if ([[listVideo objectAtIndex:indexPath.row]note] == 1) {
        cell.imageView.image = [UIImage imageNamed:@"good.png"];
    }
    if ([[listVideo objectAtIndex:indexPath.row]note] == 2) {
        cell.imageView.image = [UIImage imageNamed:@"neutre.png"];
    }
    if ([[listVideo objectAtIndex:indexPath.row]note] == 3) {
        cell.imageView.image = [UIImage imageNamed:@"bad.png"];
    }
}

if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    cell.textLabel.font=[UIFont systemFontOfSize:18.0];
    text = [NSString stringWithFormat:@"           %@",text];
}else{
    cell.textLabel.font=[UIFont systemFontOfSize:12.0];
}
if (tableView == groupListView) {
    cell.imageView.image = [UIImage imageNamed:@"group.png"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(tableView == montageListView){
    cell.imageView.image = [UIImage imageNamed:@"videos.png"];
}
cell.textLabel.text = text;
return cell;

【问题讨论】:

    标签: objective-c uitableview ios5 storyboard


    【解决方案1】:

    没有代码就不可能确切地知道问题出在哪里,但是如果您每次都重新创建表格视图,那么您需要确保在将其设置为 nil 之前执行[tableView removeFromSuperview]

    【讨论】:

    • 是的,我这样做了,所以我将它从 superview 中删除,然后我重新启动它,因此新表正确填充,但在它后面有旧表......它没有得到触摸事件但是它在那里......
    【解决方案2】:

    您的问题有点难以理解,但这是我的看法。

    您似乎想刷新表格视图“内部”的数据?如果是这样,您不需要创建新的 UITableView。只需调用reloadData,UITableView 就会查询它的委托和数据源以获取新数据。

    如果你添加一个新的 UITableView 实例,但在数据源和委托方法中返回相同的数据,它只会显示旧数据。确保更新方法。

    【讨论】:

    • 我有重新加载的数据,它也是一样的,是否有第二张表包含旧数据,我无法发布图片,所以你看到了问题,Pd:对不起我的英语 XD
    • 您是否更新了委托和数据源方法以返回新数据?
    • 能否请您发布您的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 代码。
    • 奇怪的是,当有两个表时,旧表不会对检查行的事件做出反应,如果我让它下降,单元格就会消失......
    • 好吧,如果你只想显示其他数据,重新创建整个 UITableView 是没有用的。您应该只调用reloadData,委托和数据源应该处理其余的。如果这不起作用,那么这些委托和数据源方法一定有问题。不过,如果没有看到整个代码,我无法说出确切的问题是什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 2011-01-03
    相关资源
    最近更新 更多