清除 reloadData 上的 tableView 不起作用?

发布于 2024-10-08 03:35:56 字数 2200 浏览 0 评论 0原文

我正在为我们公司编写一个简单的内部经济学应用程序,但我遇到了一些问题。我用来自动态生成的对象的信息填充 UITableView,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
Payments *project = [appDelegate.projects objectAtIndex:indexPath.row];

if([project.parentProject isEqualToString:bottomTabBar.selectedItem.title]) {
    NSLog(@"%@ är lika med %@ index: %d",project.parentProject, bottomTabBar.selectedItem.title, indexPath.row);
    // Configure the cell...
    NSMutableString *changeInValue = [[NSMutableString alloc] initWithFormat:@"%d",[[project.amountsek objectAtIndex:0] intValue]-[[project.amountsek objectAtIndex:1] intValue]];
    if([changeInValue intValue] >= 0) {
        [changeInValue insertString:@"+" atIndex:0];
        cell.imageView.image = [UIImage imageNamed:@"up.png"];
    } else {
        cell.imageView.image = [UIImage imageNamed:@"down.png"];
    }

    NSMutableString *foreignCurrency = [[NSMutableString alloc] initWithString:@""];
    if(![project.currency isEqualToString:@"SEK"]) {
        [foreignCurrency appendFormat:@" - %@%d",project.currency,[[project.payments objectAtIndex:0] intValue]];
    }

    NSString *detailString = [[NSString alloc] initWithFormat:@"%@%d (%@)%@",@"SEK",[[project.amountsek objectAtIndex:0] intValue],changeInValue, foreignCurrency];

    [changeInValue release];
    [foreignCurrency release];

    cell.textLabel.text = project.name;
    cell.detailTextLabel.text = detailString;

    [detailString release];
}

project = nil;

return cell;}

一切都像魅力一样!然而!当我按下另一个 tabButton 时,我希望它重新加载表格并仅显示匹配的元素! (匹配工作正常,日志正确打印​​出所有内容)尽管如此,在添加新单元格之前旧表格单元格不会清空。

这是重新加载 tabItem 的代码:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"Tab clicked: %d", item.tag);
[sourcesTable reloadData];
}

我该如何解决这个问题? 我是 iPhone 编程新手,我确实需要一些帮助。

I'm programming a simple in-house economics app for our company, but I'm facing some problems. I populate a UITableView with information from dynamically generated objects like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
Payments *project = [appDelegate.projects objectAtIndex:indexPath.row];

if([project.parentProject isEqualToString:bottomTabBar.selectedItem.title]) {
    NSLog(@"%@ är lika med %@ index: %d",project.parentProject, bottomTabBar.selectedItem.title, indexPath.row);
    // Configure the cell...
    NSMutableString *changeInValue = [[NSMutableString alloc] initWithFormat:@"%d",[[project.amountsek objectAtIndex:0] intValue]-[[project.amountsek objectAtIndex:1] intValue]];
    if([changeInValue intValue] >= 0) {
        [changeInValue insertString:@"+" atIndex:0];
        cell.imageView.image = [UIImage imageNamed:@"up.png"];
    } else {
        cell.imageView.image = [UIImage imageNamed:@"down.png"];
    }

    NSMutableString *foreignCurrency = [[NSMutableString alloc] initWithString:@""];
    if(![project.currency isEqualToString:@"SEK"]) {
        [foreignCurrency appendFormat:@" - %@%d",project.currency,[[project.payments objectAtIndex:0] intValue]];
    }

    NSString *detailString = [[NSString alloc] initWithFormat:@"%@%d (%@)%@",@"SEK",[[project.amountsek objectAtIndex:0] intValue],changeInValue, foreignCurrency];

    [changeInValue release];
    [foreignCurrency release];

    cell.textLabel.text = project.name;
    cell.detailTextLabel.text = detailString;

    [detailString release];
}

project = nil;

return cell;}

And everything works like a charm! However! When I press another tabButton I want it to reload the table and to display only the matched elements! (The matching works fine, the log prints out everything correctly) Although, the old table cells does not empty before the new ones are added.

Here's the code for the reload tabItem:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"Tab clicked: %d", item.tag);
[sourcesTable reloadData];
}

How do I solve this?
I'm new to programming for the iPhone and I could really use some help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很OK 2024-10-15 03:35:56

请调用[sourcesTable reloadData];在 ViewController.m 的 viewWillAppear 方法中
每当您的视图出现时都会出现此调用。

Please call [sourcesTable reloadData]; in viewWillAppear method of that ViewController.m
This call every time when your view is appears.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文