2011-10-31 50 views
0

有一個UITableView,它的數據源是一個NSMutableArray,它實際上是一個解析的XML文件;在同一視圖中,我有兩個具有相同操作方法的UIButton,我如何能夠切換tableview的數據源?用兩個UIButtons更改tableview數據源

當按下A按鈕來加載dataSource1和B加載dataSource2

- (void)buttonsAction: (id)sender { 
     
    BOOL activateSecond = _firstButton.selected; 
    _firstButton.selected = !activateSecond; 
    _secondButton.selected = activateSecond; 
} 

    dataSource1 = [[NSMutableArray alloc]init]; 
    nodecontent = [[NSMutableString alloc]init]; 
         
    NSString *xmlURL = @"http://mydomain.com/file.xml"; 
    NSData *xmlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:xmlURL]]; 
    xmlParserObject = [[NSXMLParser alloc]initWithData:xmlData]; 
    [xmlParserObject setDelegate:self]; 
    [xmlParserObject parse]; 
    [xmlData release]; 

任何線索?

回答

4

步驟1:將數據加載到你的陣列(如果你是從XML /網頁抓取做到這一點,我已經實現這裏靜態)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.arOne=[NSArray arrayWithObjects:@"Sagar",@"Amit",@"Nimit",@"Paresh",@"Rakesh",@"Yogesh", nil]; 
    self.arTwo=[NSArray arrayWithObjects:@"Supreeth",@"Deepthy",@"Ankit",@"Sandeep",@"Gomathy", nil]; 
    [self.tableView reloadData]; 
} 

第2步:tableViewCell

地方條件編碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    cell.textLabel.text=[(self.btnTapped.selected)?self.arTwo:self.arOne objectAtIndex:indexPath.row]; 
    return cell; 
} 

第3步:一個動作連接到您的按鈕(此按鈕命名爲btnTapped

- (IBAction)btnTitle:(id)sender { 
    self.btnTapped.selected=!self.btnTapped.selected; 
    [self.tableView reloadData]; 
} 
+0

謝謝爲您的文章!我的情況更復雜,但我想我會在一天結束時解決問題 –

+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/4614/discussion-between-糖和-EL-塞維羅) –

0

當你點擊第二個按鈕,從數據源陣列中刪除所有的元素,如

[dataSource1 removeAllObjects]; 

然後解析XML和數據添加到這些陣列,並通過做

解析當拿完刷新你的tableView
[tbl reloadData]; 

希望它可以幫助....

+0

爲nodecontent這是一個NSMutableArray它說'removeAllObjects'可能不會響應... –

+0

編輯我的答案,我認爲nodecontent作爲一個NSMutableArray ... –