【问题标题】:Change tableview datasource with two UIButtons使用两个 UIButton 更改 tableview 数据源
【发布时间】:2011-10-31 12:27:04
【问题描述】:

有一个 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];

有什么线索吗?

【问题讨论】:

    标签: iphone uibutton ios5 uitableview


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

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

    [dataSource1 removeAllObjects];  
    

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

    [tbl reloadData];
    

    希望对你有帮助....

    【讨论】:

    • 对于 NSMutableArray 的节点内容,它说“removeAllObjects”可能没有响应......
    猜你喜欢
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2018-08-15
    相关资源
    最近更新 更多