0

I have a view with a tabBar at the bottom and a tableview. When a barButtonItem is pressed, the array that holds the data for the tableview changes.

With NSLogs, it is clear that the array is really changing, as the [NSArray count] displays different values. However, when I use [UITableview reloadData], the cells stay the same.

If I scroll a up a bit however and then scroll back down, whatever went offscreen gets updating. I'm guessing this is because when it goes offscreen it is dequeued and when it comes back it is redrawn with the new data. Is there a way to just have it redraw everything?

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController
@synthesize listBar,selectedTab , linkTableView, barButton5, barButton4, barButton3, barButton2, barButton1, Lists, imageID, titleID, linkID, cells;


- (void)viewDidLoad
{
    [super viewDidLoad];
    linkTableView = [[UITableView    alloc] init];
    Lists         = [[NSArray        alloc] init];
    imageID       = [[NSMutableArray alloc] init];
    titleID       = [[NSMutableArray alloc] init];
    linkID        = [[NSMutableArray alloc] init];
    linkTableView.delegate   = self;
    linkTableView.dataSource = self;
}

-(void)viewWillAppear:(BOOL)animated{
    NSArray *barItems = [[NSArray alloc] initWithObjects:barButton1, barButton2, barButton3, barButton4, barButton5, nil];
    listBar.selectedItem = [barItems objectAtIndex:selectedTab];

    //when view will appear load data dependent on selectedTab
    [self performSelector:@selector(updateTable)];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    selectedTab = item.tag;
    [self performSelector:@selector(updateTable)];
}

-(void)updateTable{
    [imageID removeAllObjects];
    [titleID removeAllObjects];
    [linkID removeAllObjects];


    //load from the xml file
    RXMLElement *rxml = [RXMLElement elementFromXMLFile:@"Lists.xml"];
    //makes an array from the list children
    Lists = [rxml children:@"List"];
    cells = [[Lists objectAtIndex:selectedTab] children:@"Cell"];
    [rxml iterateElements:cells usingBlock:^(RXMLElement *cellElement) {
        [imageID addObject:[cellElement child:@"ImageName"]];
        [titleID addObject:[cellElement child:@"CellText" ]];
        [linkID  addObject:[cellElement child:@"Link"     ]];
    }];

    NSLog(@"Count is %i", [cells count]);
    [linkTableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [cells count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

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

    static NSString *CellIdentifier = @"LinkCell";
    linkCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[linkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *imageName = [NSString stringWithFormat:@"%@", [imageID objectAtIndex:indexPath.row]];
    cell.image.image = [UIImage imageNamed:imageName];
    NSString *labelText = [NSString stringWithFormat:@"%@", [titleID objectAtIndex:indexPath.row]];
    cell.linkCellLabel.text = labelText;
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
4

1 回答 1

0

从您的代码中删除这一行:
linkTableView = [[UITableView alloc] init];

或您的 uitableview 的任何其他初始化程序,并使用此代码更新部分

  [self.tableView beginUpdates];
NSMutableIndexSet* index = [[NSMutableIndexSet alloc]init];
[index addIndex:0];
[self.tableView reloadSections:index withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
于 2013-09-26T12:40:42.467 回答