2009-07-15 254 views
4

我有一个NSURL对象,它根据用户输入到搜索栏中的变量从我的网站获取数据。动态更改UITableView的内容

我把这个数据分成一个NSArray。

一旦我这样做了,我希望在UITableView中显示数据。

我的问题是这样的。是否可以将数据动态加载到UITableView中?

即程序加载,没有数据,所以UITableView为空,则用户搜索一个变量。获取一些数据并将其内容加载到UITableView中。搜索一个新变量,从UITableView清除旧数据并添加新数据?

我目前正在尝试使用接口生成器来做到这一点,但担心我可能不得不实际地使我的接口,以便我可以销毁并重新创建UITableView,但我不知道。

感谢您的任何帮助。

回答

7

当然在UITableView的方法reloadData会做的伎俩

+0

要澄清添加到我原来的职位,为什么我不认为我可以使用reloadData。 – JonB 2009-07-15 14:08:54

3

不要害怕,子类的UITableView是很容易的。在xCode中,只需选择新文件,选择“Cocoa Touch Classes”,“Objective-c class”,然后在“Subclass of”下拉选择“UITableView”。 xCode将添加一个UITableViewController子类,并完成存根的构建。

我填写了一个非常简单的示例,该示例从数组中绘制表格数据并从应用程序委托中显示。正如你所建议的那样,向UITableView发送reloadData消息将刷新显示的数据。

正如您可能已经发现的那样,使用InterfaceBuilder进行此项工作比以编程方式难得多。

干杯,尼尔斯

// 
// MyTableViewController.m 
// TableView 
// 
// Created by Niels Castle on 7/15/09. 
// Copyright 2009 Castle Andersen ApS. All rights reserved. 
// 

#import "MyTableViewController.h" 


@implementation MyTableViewController 

// Initializer do custom initialisation here 
- (id)initWithStyle:(UITableViewStyle)style { 
    if (self = [super initWithStyle:style]) { 

     // This is the source of my data. The simplest source possible, 
     // an NSMutableArray, of name. This would be the data from your web site 
     array = [[NSMutableArray alloc] 
     initWithObjects:@"Niels", @"Camilla", @"William", nil]; 
    } 
    return self; 
} 


// How many section do we want in our table 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view 
// Simply the number of elements in our array of names 
- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section { 
    return [array count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Reuse cells 
    static NSString *id = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] 
      initWithStyle: UITableViewCellStyleDefault 
      reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Simplest possible cell - displaying a name from our name array 
    [[cell textLabel] setText: [array objectAtIndex:[indexPath row]]]; 

    return cell; 
} 

- (void)dealloc { 
    [super dealloc]; 
    [array release]; 
} 

@end 


// 
// TableViewAppDelegate.m 
// TableView 
// 
// Created by Niels Castle on 7/15/09. 
// Copyright Castle Andersen ApS 2009. All rights reserved. 
// 

#import "TableViewAppDelegate.h" 
#import "MyTableViewController.h" 

@implementation TableViewAppDelegate 

@synthesize window; 


- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    MyTableViewController *twc = [[MyTableViewController alloc] 
     initWithStyle: UITableViewStylePlain]; 
    [window addSubview: [twc view]]; 

    [window makeKeyAndVisible]; 
} 


- (void)dealloc { 
    [window release]; 
    [super dealloc]; 
} 


@end 
0

这是一个有点复杂,但我的解决方案,可靠地工作很是以下几点: (假设你有一个数组作为一群阵列,每一个代表一个部分,包含实际上是表中的行的项目)。

这个例子符合这种情况,当我们从服务器(例如JSON)加载一些数据时,结果可能在段和/或行数上有很大不同。

void函数,你可以省略

-(void)addToPropertiesTable { 

    //fullTableData is above mentioned two dimensional array 
    int sectionsCount = _fullTableData.count; 
    int count = 0; 
    NSMutableArray *insertIndexPaths = [NSMutableArray array]; 
    NSMutableArray *deleteIndexPaths = [NSMutableArray array]; 

    for(int j = 0; j < sectionsCount; j++) { 
     NSMutableArray *currentAdverts = [[NSMutableArray alloc] init]; 
     [currentAdverts addObjectsFromArray:[_fullTableData objectAtIndex:j]]; 
     count = [currentAdverts count]; 

     int currentRowsInSection = [self.propertiesTable numberOfRowsInSection:j]; 

     if(currentRowsInSection > 0) { 
      //if any data in current tableView, lets get rid of them first 
      for (int i = [self.propertiesTable numberOfRowsInSection:j] - 1; i >=0 ; i--) 
      { 
       [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:j]]; 
      } 
     } 
     for (NSUInteger item = 0; item < count; item++) {    
      [insertIndexPaths addObject:[NSIndexPath indexPathForRow:item inSection:j]]; 
     } 
    } 


    [self.propertiesTable beginUpdates]; 

    //we delete old rows - whether we need them or not 
    [self.propertiesTable deleteRowsAtIndexPaths:deleteIndexPaths 
           withRowAnimation:UITableViewRowAnimationFade]; 
    if([self.propertiesTable numberOfSections]) { 

     //if any sections, we remove them 
     NSIndexSet *nsIndexSetToDelete = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[self.propertiesTable numberOfSections])]; 
     [self.propertiesTable deleteSections:nsIndexSetToDelete withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 


    //here we have to set new sections, whether they have changed or not 
    NSIndexSet *nsIndexSetToInsert = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,sectionsCount)]; 
    [self.propertiesTable insertSections:nsIndexSetToInsert withRowAnimation:UITableViewRowAnimationAutomatic]; 

    //finally we insert rows 
    [self.propertiesTable insertRowsAtIndexPaths:insertIndexPaths 
           withRowAnimation:UITableViewRowAnimationFade]; 
    [self.propertiesTable endUpdates]; 

    //now we see the change in UI 
    [self.propertiesTable reloadData]; 
}