2009-07-15 255 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]; 
}