Reputation: 53
I am trying to create a Table View in which the data source can be changed depending on which Index is selected on the UISegmentedControl. I will post what I have. It doesnt seem to be working! I want to do it like Twitter does on their profile page with the "Tweets, Media Favourites" segmented Controller.
-(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];
}
}
I am using a Custom Segmented COntrol, but willing to change to the Regular one. If you have any insight or questions, please let me know!
Thank you very much!!
Upvotes: 0
Views: 510
Reputation: 2343
Two things.
First you should clear the data before you updated the cells
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;
}
Second, in segmentedControlIndexChanged you are running same code in different if statements. The following should do enough
-(void)segmentedControlIndexChanged:(HMSegmentedControl *)segmentedControl{
self.hotBoxTableView.hidden = NO;
[self.hotBoxTableView reloadData];
}
Upvotes: 1