繁体   English   中英

使用两个UIButton更改tableview数据源

[英]Change tableview datasource with two UIButtons

具有UITableView及其数据源的是NSMutableArray,它实际上是一个已解析的XML文件; 在同一视图中,我有两个具有相同操作方法的UIButton,如何切换表视图的数据源?

当按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];

有什么线索吗?

第1步:将数据加载到您的数组中(如果您是从xml / web获取的,那么我已经在这里实现了static)

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

当您单击第二个按钮时,从数据源数组中删除所有元素,例如

[dataSource1 removeAllObjects];  

然后解析xml并将数据添加到这些数组,解析完成后,通过执行以下操作重新加载tableView

[tbl reloadData];

希望能帮助到你....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM