开发者

Clear the tableView on reloadData not working?

开发者 https://www.devze.com 2023-01-31 04:02 出处:网络
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:

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] initW开发者_运维知识库ithFormat:@"%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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号