ios - 按下按钮时显示标题而不是段控制ios
<p><p>我有一个表格 View ,可以通过工具栏中的按钮加载不同的数据。所以我想做的是隐藏段控制并在按下某个按钮时显示标题,反之亦然,如果按下另一个按钮。 </p>
<p>我的段控件被命名为 sortButton,我把它隐藏了</p>
<pre><code>sortButton.hidden = TRUE
</code></pre>
<p>并显示它</p>
<pre><code>sortButton.hidden = FALSE
</code></pre>
<p>所以当按钮被隐藏时,我想在其位置放置标题。知道如何解决这个问题。</p>
<p>我尝试过简单的</p>
<pre><code>self.title = @"Restavracije";
</code></pre>
<p>或</p>
<pre><code>self.navigationItem.title = @"Restavracije";
</code></pre>
<p>但标题没有出现</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我为您制作了一个简约的演示项目,说明了如何完成您想要的。</p>
<p>这是相关代码:</p>
<pre><code>@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 {
;
// Create the segmented control
self.sortButton = [ initWithItems:@[@"First", @"Second", @"Hide me"]];
;
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 = [ initWithArray:self.dataSource1];
;
self.dataSource3 = tempDataSourceArray3;
}
- (void)switchDataInTableView {
// Reload the table view.
// tableView:cellForRowAtIndexPath decides which datasource to show
;
// 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 = ;
// If "First" is selected we want the text to be taken fromt the dataSource1 array
tableViewCell.textLabel.text = self.sortButton.selectedSegmentIndex == 0 ? self.dataSource1 : self.sortButton.selectedSegmentIndex == 1 ? self.dataSource2 : self.dataSource3;
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
</code></pre>
<p> <a href="https://dl.dropboxusercontent.com/u/50800334/Demo.zip" rel="noreferrer noopener nofollow">Download it here</a> </p></p>
<p style="font-size: 20px;">关于ios - 按下按钮时显示标题而不是段控制ios,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20173708/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20173708/
</a>
</p>
页:
[1]