UITableview在重新加载数据后保留旧单元格

UITableview在重新加载数据后保留旧单元格,uitableview,scroll,reloaddata,Uitableview,Scroll,Reloaddata,我有一个底部有一个tabBar的视图和一个tableview。按下按钮时,保存tableview数据的数组将发生更改 有了NSLogs,很明显阵列确实在变化,因为[NSArray count]显示了不同的值。但是,当我使用[UITableview reloadData]时,单元格保持不变 然而,如果我向上滚动一点,然后向下滚动,屏幕外的任何内容都会得到更新。我猜这是因为当它离开屏幕时,它会退出队列,当它回来时,它会用新数据重新绘制。有没有办法让它重画所有东西 #import "TableView

我有一个底部有一个tabBar的视图和一个tableview。按下按钮时,保存tableview数据的数组将发生更改

有了NSLogs,很明显阵列确实在变化,因为[NSArray count]显示了不同的值。但是,当我使用[UITableview reloadData]时,单元格保持不变

然而,如果我向上滚动一点,然后向下滚动,屏幕外的任何内容都会得到更新。我猜这是因为当它离开屏幕时,它会退出队列,当它回来时,它会用新数据重新绘制。有没有办法让它重画所有东西

#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
#导入“TableViewController.h”
@接口TableViewController()
@结束
@TableViewController的实现
@合成列表栏、selectedTab、linkTableView、barButton5、barButton4、barButton3、barButton2、barButton1、列表、imageID、titleID、linkID、单元格;
-(无效)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;
}
-(无效)视图将显示:(BOOL)动画{
NSArray*barItems=[[NSArray alloc]initWithObjects:barButton1、barButton2、barButton3、barButton4、barButton5、nil];
listBar.selectedItem=[barItems objectAtIndex:selectedTab];
//显示视图时,根据所选选项卡加载数据
[自执行选择器:@selector(updateTable)];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(void)选项卡栏:(UITabBar*)选项卡栏didSelectItem:(UITabBarItem*)项{
selectedTab=item.tag;
[自执行选择器:@selector(updateTable)];
}
-(void)可更新{
[imageID removeAllObjects];
[标题删除所有对象];
[linkID removeAllObjects];
//从xml文件加载
RXMLElement*rxml=[RXMLElement elementFromXMLFile:@“Lists.xml”];
//从列表子项生成数组
Lists=[rxml子项:@“List”];
cells=[[Lists objectAtIndex:selectedTab]子项:@“Cell”];
[rxml迭代元素:使用块的单元格:^(RXMLElement*cellElement){
[imageID addObject:[cellElement子项:@“ImageName”];
[标题ID addObject:[cellElement子项:@“CellText”];
[linkID addObject:[cellElement子项:@“链接”];
}];
NSLog(@“计数为%i,[单元计数]);
[linkTableView重新加载数据];
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
返回[单元格计数];
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
返回60;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“LinkCell”;
linkCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
如果(单元格==nil){
cell=[[linkCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//配置单元格。。。
NSString*imageName=[NSString stringWithFormat:@“%@,[imageID objectAtIndex:indexPath.row]];
cell.image.image=[UIImage imageName:imageName];
NSString*labelText=[NSString stringWithFormat:@“%@,[titleID objectAtIndex:indexPath.row]];
cell.linkcelllable.text=labelText;
返回单元;
}
/*
//替代以支持表视图的条件编辑。
-(BOOL)tableView:(UITableView*)tableView caneditrowatinexpath:(nsindepath*)indepath
{
//如果不希望指定的项可编辑,则返回“否”。
返回YES;
}
*/
/*
//替代以支持编辑表格视图。
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径
{
如果(editingStyle==UITableViewCellEditingStyleDelete){
//从数据源中删除该行
[tableView deleteRowsAtIndexPaths:@[indexPath]和RowAnimation:UITableViewRowAnimationFade];
}   
else if(editingStyle==UITableViewCellEditingStyleInsert){
//创建相应类的新实例,将其插入数组,并向表视图添加新行
}   
}
*/
/*
//替代以支持重新排列表视图。
-(void)tableView:(UITableView*)tableView移动rowatinexpath:(nsindepath*)从indepath到indepath:(nsindepath*)到indepath
{
}
*/
/*
//重写以支持表视图的条件重新排列。
-(BOOL)tableView:(UITableView*)tableView可以移动rowatinexpath:(nsindepath*)indepath
{
//如果不希望该项目可重新订购,则返回“否”。
返回YES;
}
*/
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
//导航逻辑可能位于此处。创建并推送另一个视图控制器。
/*
*detailViewController=[[alloc]initWithNibName:@“bundle:nil];
// ...
//将选定对象传递给新的视图控制器。
[self.navigationController pushViewController:detailViewController动画:是];
*/
}
@结束

从代码中删除此行:
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];