使用两个UIButton更改tableview数据源

时间:2011-10-31 12:27:05

标签: iphone uibutton ios5 uitableview

拥有UITableView及其数据源是一个NSMutableArray,它实际上是一个解析的XML文件;在同一视图中,我有两个UIButton具有相同的操作方法,我如何切换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];

有任何线索吗?

2 个答案:

答案 0 :(得分:4)

第1步:将数据加载到数组中(如果从xml / web获取,请执行此操作,我已在此处实现静态)

- (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];
}

答案 1 :(得分:0)

单击第二个按钮时,删除数据源阵列中的所有元素,如

[dataSource1 removeAllObjects];  

然后解析xml并将数据添加到这些数组中,并在解析完成后重新加载tableView

[tbl reloadData];

希望它有所帮助......

相关问题