OGeek|极客世界-中国程序员成长平台

标题: ios - 按下按钮时显示标题而不是段控制ios [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:40
标题: ios - 按下按钮时显示标题而不是段控制ios

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

我的段控件被命名为 sortButton,我把它隐藏了

sortButton.hidden = TRUE 

并显示它

sortButton.hidden = FALSE 

所以当按钮被隐藏时,我想在其位置放置标题。知道如何解决这个问题。

我尝试过简单的

self.title = @"Restavracije";

self.navigationItem.title = @"Restavracije"; 

但标题没有出现



Best Answer-推荐答案


我为您制作了一个简约的演示项目,说明了如何完成您想要的。

这是相关代码:

@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 actionselector(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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)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

关于ios - 按下按钮时显示标题而不是段控制ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20173708/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4