使用 UISegmentedControl 更改 UITableView 的数据源(就像 Twitter 那样)
Changing datasource of UITableView using a UISegmentedControl (Like Twitter does)
我正在尝试创建一个 Table 视图,其中的数据源可以根据在 UISegmentedControl 上选择的索引进行更改。我会 post 我有什么。它似乎没有用!我想像 Twitter 在其配置文件页面上使用 "Tweets, Media Favourites" 分段控制器那样做。
-(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];
}
}
我正在使用自定义分段控件,但愿意更改为常规控件。如果您有任何见解或问题,请告诉我!
非常感谢!!
两件事。
首先你应该在更新单元格之前清除数据
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];
}
我正在尝试创建一个 Table 视图,其中的数据源可以根据在 UISegmentedControl 上选择的索引进行更改。我会 post 我有什么。它似乎没有用!我想像 Twitter 在其配置文件页面上使用 "Tweets, Media Favourites" 分段控制器那样做。
-(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];
}
}
我正在使用自定义分段控件,但愿意更改为常规控件。如果您有任何见解或问题,请告诉我!
非常感谢!!
两件事。
首先你应该在更新单元格之前清除数据
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];
}