按下按钮时显示标题而不是段控制

时间:2013-11-24 10:55:32

标签: ios objective-c

我有一个表格视图,可以通过工具栏中的按钮加载不同的数据。所以我想要做的是隐藏段控件并在按下某个按钮时显示标题,反之亦然如果按下另一个按钮。

我的段控件名为sortButton,我将其隐藏

sortButton.hidden = TRUE 

并以

显示
sortButton.hidden = FALSE 

因此,当按钮被隐藏时,我想在它的位置上有标题。任何想法如何解决这个问题。

我尝试过简单的

self.title = @"Restavracije";

self.navigationItem.title = @"Restavracije"; 

但标题不会出现

1 个答案:

答案 0 :(得分:1)

我为你制作了一个简约的演示项目,展示了如何实现你想要的目标。

这是相关代码:

@interface HASTableViewController ()
@property (strong, nonatomic) UISegmentedControl *sortButton;
@property (copy, nonatomic) NSArray *dataSource1;
@property (copy, nonatomic) NSArray *dataSource2;
@property (copy, nonatomic) NSArray *dataSource3;
@end

@implementation HASTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Create the segmented control
    self.sortButton = [[UISegmentedControl alloc] initWithItems:@[@"First", @"Second", @"Hide me"]];
    [self.sortButton addTarget:self action:@selector(switchDataInTableView) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.sortButton;
    self.sortButton.selectedSegmentIndex = 0;
    // We set the title you want to show when the segmented control is "hidden"
    self.navigationItem.title = @"Sort Button is nil.";
    // Setup a data source
    self.dataSource1 = @[@"First", @"First", @"First", @"First", @"First", @"First", @"First", @"First", @"First", @"First", @"First"];
    // and another one
    self.dataSource2 = @[@"Second", @"Second", @"Second", @"Second", @"Second"];
    // Create a third datasource which contains both arrays
    NSMutableArray *tempDataSourceArray3 = [[NSMutableArray alloc] initWithArray:self.dataSource1];
    [tempDataSourceArray3 addObjectsFromArray:self.dataSource2];
    self.dataSource3 = tempDataSourceArray3;
}

- (void)switchDataInTableView {
    // Reload the table view.
    // tableView:cellForRowAtIndexPath decides which datasource to show
    [self.tableView reloadData];
    // If it is "Hide it" we hide
    if (self.sortButton.selectedSegmentIndex == 2) self.navigationItem.titleView = nil;
}

#pragma mark - UITableView Data Source Methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // We use the standard cell
    UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    // If "First" is selected we want the text to be taken fromt the dataSource1 array
    tableViewCell.textLabel.text = self.sortButton.selectedSegmentIndex == 0 ? self.dataSource1[indexPath.item] : self.sortButton.selectedSegmentIndex == 1 ? self.dataSource2[indexPath.item] : self.dataSource3[indexPath.item];
    return tableViewCell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // return number of rows
    return self.sortButton.selectedSegmentIndex == 0 ? self.dataSource1.count : self.sortButton.selectedSegmentIndex == 1 ? self.dataSource2.count : self.dataSource3.count;
}

@end

Download it here