简体   繁体   English

使用两个UIButton更改tableview数据源

[英]Change tableview datasource with two UIButtons

having a UITableView and its data source is an NSMutableArray which is actually an parsed XML file; 具有UITableView及其数据源的是NSMutableArray,它实际上是一个已解析的XML文件; In the same view I have two UIButtons with the same action method, how I'm able to toggle the tableview's datasource? 在同一视图中,我有两个具有相同操作方法的UIButton,如何切换表视图的数据源?

When pressing A button to load dataSource1 and B to load dataSource2 当按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];

Any clue? 有什么线索吗?

Step 1 : load data into your arrays ( if you are fetching from xml/web do that, I have implemented static here ) 第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];
}

step 2 : place conditional coding for tableViewCell 步骤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;
}

step 3 : connect an action to your button ( here button is named as btnTapped ) 步骤3:将动作连接到您的按钮(此处的按钮名为btnTapped

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

When you click second button , remove all the elements from your data source arrays like 当您单击第二个按钮时,从数据源数组中删除所有元素,例如

[dataSource1 removeAllObjects];  

And then parse the xml and add data to these arrays and when parsing get finished reload your tableView by doing 然后解析xml并将数据添加到这些数组,解析完成后,通过执行以下操作重新加载tableView

[tbl reloadData];

Hope it helps.... 希望能帮助到你....

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

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