【问题标题】:Changing datasource of UITableView using a UISegmentedControl (Like Twitter does)使用 UISegmentedControl 更改 UITableView 的数据源(就像 Twitter 一样)
【发布时间】:2015-03-12 05:33:33
【问题描述】:

我正在尝试创建一个表视图,其中数据源可以根据在 UISegmentedControl 上选择的索引进行更改。我会发布我所拥有的。它似乎没有工作!我想像 Twitter 在他们的个人资料页面上使用“推文、媒体收藏夹”分段控制器那样做。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    switch (self.segmentedControliPhone4.selectedSegmentIndex) {
        case 0:
            return 10;
            break;
        case 1:
            return 15;
            break;
        case 2:
            return 5;
            break;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    self.segmentedControliPhone4 = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"ACTIVITY FEED", @"HOT BOX", @"COLLECTIONS"]];
    self.segmentedControliPhone4.selectedSegmentIndex = 1;
    self.segmentedControliPhone4.frame = CGRectMake(0, 263, self.view.frame.size.width, 30);
    self.segmentedControliPhone4.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segmentedControliPhone4.selectionStyle = HMSegmentedControlSelectionStyleBox;
    self.segmentedControliPhone4.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
    self.segmentedControliPhone4.backgroundColor = [UIColor blackColor];
    self.segmentedControliPhone4.textColor = [UIColor whiteColor];
    self.segmentedControliPhone4.font = [UIFont fontWithName:@"Lato" size:10];
    self.segmentedControliPhone4.selectedTextColor = [UIColor colorWithRed:0.016 green:0.850 blue:0.796 alpha:1];
    self.segmentedControliPhone4.selectionIndicatorColor = [UIColor colorWithRed:0.016 green:0.850 blue:0.796 alpha:1];
    [self.segmentedControliPhone4 addTarget:self action:@selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];
    return self.segmentedControliPhone4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    switch (self.segmentedControliPhone4.selectedSegmentIndex) {
        case 0:
            cell.textLabel.text = @"Acitivy Feed";
            cell.imageView.image = [UIImage imageNamed:@"brand.png"];
            break;
        case 1:
            cell.textLabel.text = @"Hot Box";
            break;
        case 2:
            cell.textLabel.text = @"Collections";
            break;
    }

    return cell;
}

-(void)segmentedControlIndexChanged:(HMSegmentedControl *)segmentedControl{

    if(segmentedControl.selectedSegmentIndex == 0){
        self.hotBoxTableView.hidden = NO;
        [self.hotBoxTableView reloadData];
    }
    else if(segmentedControl.selectedSegmentIndex == 1){
        self.hotBoxTableView.hidden = NO;
        [self.hotBoxTableView reloadData];
    }
    else if(segmentedControl.selectedSegmentIndex == 2){
        self.hotBoxTableView.hidden = NO;
        [self.hotBoxTableView reloadData];
    }
}

我正在使用自定义分段控制,但愿意更改为常规控制。如果您有任何见解或问题,请告诉我!

非常感谢!!

【问题讨论】:

  • 什么不起作用?此外,您的问题的标题包含一个很好的设计理念:您实际上可以通过更改其 datasource 属性来更改 tableview 的数据源。
  • 分段控件不会更改值(它始终保持突出显示),并且 tableview 内的数据与突出显示的数据和您手动选择的索引交叉。所以它试图显示两个数据源。
  • 1- 我可能会从修复控件开始。如果控制表视图的东西坏了,那么在担心表本身之前解决这个问题。 2-我猜您的表格视图“交叉”数据是由于您在选择索引 1 或 2 的情况下没有重置图像。选择新索引时单元格的数量是否正确更新?

标签: ios objective-c iphone uitableview uisegmentedcontrol


【解决方案1】:

两件事。

首先你应该在更新单元格之前清除数据

cell.textLabel.text = nil
cell.imageView.image = nil
switch (self.segmentedControliPhone4.selectedSegmentIndex) {
    case 0:
        cell.textLabel.text = @"Acitivy Feed";
        cell.imageView.image = [UIImage imageNamed:@"brand.png"];
        break;
    case 1:
        cell.textLabel.text = @"Hot Box";
        break;
    case 2:
        cell.textLabel.text = @"Collections";
        break;
}

其次,在 segmentedControlIndexChanged 中,您在不同的 if 语句中运行相同的代码。以下应该足够了

-(void)segmentedControlIndexChanged:(HMSegmentedControl *)segmentedControl{

    self.hotBoxTableView.hidden = NO;
    [self.hotBoxTableView reloadData];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2023-04-05
    • 2011-05-15
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多