2011-10-31 53 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 ... –