通过UISegmentedControl切换数据源后重新加载UITableView

时间:2015-05-18 15:14:55

标签: ios objective-c uitableview

我的UIViewController包含UISegmentedControlUITableViewController

段表示方向 - 即北/南。

这个想法是,当触摸两个段中的一个时,将加载相应的数据。

[self.tableView reloadData]不起作用。

我发现正确加载数据的唯一方法是拖动tableView内容直到当前单元格失焦 - 然后,加载正确的数据。

非常感谢帮助。 为清晰起见,我添加了UIDataSource方法:

- (IBAction)segmentAction:(id)sender
{
    //All data is loaded into two arrays in ViewDidLoad.
    [self.tableView reloadData];    
}



#pragma mark - tableView

//Number of sections: 1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (directionControl.selectedSegmentIndex == 0) return routeDirection0.count;
    return routeDirection1.count;
}


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

    static NSString *CellIdentifier = @"DetailCellIdentifier";

    RouteDetailCell *cell = (RouteDetailCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RouteDetailCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    //currentRoute is an initialized mutable array
    if (self.directionControl.selectedSegmentIndex == 0) {
        currentRoute = [routeDirection0 objectAtIndex:indexPath.row];
    }
    if (self.directionControl.selectedSegmentIndex == 1) {
        currentRoute = [routeDirection1 objectAtIndex:indexPath.row];
    }

    NSString *tempString = [currentRoute valueForKey:@"Name" ];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@", tempString];
    return cell;
}

UPDATE - 根据要求,我设置了一个断点,以便在点击段控件时检查tableView的状态。我注意到的一件事是框架尺寸为零..

2015-05-18 12:23:10.188 Transit[14488:3166122] -[RouteDirectionViewController segmentAction:]
2015-05-18 12:23:10.189 Transit[14488:3166122] tableView: <UITableView: 0x7f9f6e057200; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9f6b44b2d0>; layer = <CALayer: 0x7f9f6b44d160>; contentOffset: {0, 0}; contentSize: {0, 0}>
2015-05-18 12:23:12.494 Transit[14488:3166122] -[RouteDirectionViewController segmentAction:]
2015-05-18 12:23:12.494 Transit[14488:3166122] tableView: <UITableView: 0x7f9f6e057200; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f9f6b44b2d0>; layer = <CALayer: 0x7f9f6b44d160>; contentOffset: {0, 0}; contentSize: {0, 0}>

1 个答案:

答案 0 :(得分:0)

我最终做的是创建两个UITableViews并交换这些而不是数据。

所涉及的全部是:

  1. 连接数据源并委托两个tableViews。
  2. numberOfRowsInSection:中,检查正在使用的表格(基于分段控制索引)。
  3. cellForRowAtIndexPath
  4. 中执行相同操作
相关问题