Search Controller & Search Bar & Refresh control...
@interface TableViewController : UITableViewController<UISearchResultsUpdating,UISearchBarDelegate>
@property UISearchController *SC;
@property NSArray *filteredAArray;
@property NSArray *filteredBArray;
@property NSArray *filteredCArray;
@property NSMutableArray *studentsArray;
@property NSMutableArray *letterAArray;
@property NSMutableArray *letterBArray;
@property NSMutableArray *letterCArray;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.studentsArray=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"G",@"H",@"P",@"R",@"S",@"T",@"V", nil];
self.letterAArray=[[NSMutableArray alloc]initWithObjects:@"Arun",@"Arjun",@"Amar",@"Ajay",@"Abhiash", nil];
self.letterBArray=[[NSMutableArray alloc]initWithObjects:@"Bharath",@"Babu",@"Bhavya",@"Balu", nil];
self.letterCArray=[[NSMutableArray alloc]initWithObjects:@"Chakri",@"Charan",@"Chaitu",@"Chintu", nil];
//Search Controller & Search Bar
self.SC=[[UISearchController alloc]initWithSearchResultsController:nil];
self.tableView.tableHeaderView=self.SC.searchBar;
self.SC.searchResultsUpdater=self;
self.SC.searchBar.delegate=self;
// Refresh Control
self.refreshControl=[[UIRefreshControl alloc]init];
[self.refreshControl addTarget:self action:@selector(dataFromServer) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:self.refreshControl];
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
{
NSPredicate * predicate=[NSPredicate predicateWithFormat:@"SELF contains[c]%@",searchController.searchBar.text];
self.filteredAArray=[self.letterAArray filteredArrayUsingPredicate:predicate];
self.filteredBArray=[self.letterBArray filteredArrayUsingPredicate:predicate];
self.filteredCArray=[self.letterCArray filteredArrayUsingPredicate:predicate];
[self.tableView reloadData];
}
-(void)dataFromServer {
[self.refreshControl endRefreshing];
}
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// return list of section titles to display in section index view (e.g. "ABCD...Z#")
return self.studentsArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.studentsArray count];
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(self.SC.active==YES)
{
return @"";
}
NSMutableString *titleStr;
if (section==0) {
titleStr=[self.studentsArray objectAtIndex:0];
} else if (section==1) {
titleStr=[self.studentsArray objectAtIndex:1];
} else if (section==2) {
titleStr=[self.studentsArray objectAtIndex:2];
}
return titleStr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section==0)
{
if(self.SC.active==YES) {
return self.filteredAArray.count;
} else {
return self.letterAArray.count;
}
} else if(section==1) {
if(self.SC.active==YES) {
return self.filteredBArray.count;
} else {
return self.letterBArray.count;
}
}else if(section==2) {
if(self.SC.active==YES) {
return self.filteredCArray.count;
} else {
return self.letterCArray.count;
}
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"cellID"];
}
if (indexPath.section==0) {
if (self.SC.active==YES) {
cell.textLabel.text=[self.filteredAArray objectAtIndex:indexPath.row];
}
cell.textLabel.text=[self.letterAArray objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor blueColor];
cell.backgroundColor=[UIColor brownColor];
}
else if (indexPath.section==1) {
if (self.SC.active==YES) {
cell.textLabel.text=[self.filteredBArray objectAtIndex:indexPath.row];
}
cell.textLabel.text=[self.letterBArray objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor blueColor];
cell.backgroundColor=[UIColor purpleColor];
}
else if (indexPath.section==2) {
if (self.SC.active==YES) {
cell.textLabel.text=[self.filteredCArray objectAtIndex:indexPath.row];
}
cell.textLabel.text=[self.letterCArray objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor blueColor];
cell.backgroundColor=[UIColor brownColor];
}
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
if (indexPath.section==1) {
if ([self.letterBArray objectAtIndex:indexPath.row]) {
return NO;
}
}
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
if (indexPath.section==2) {
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.letterCArray removeObjectAtIndex:indexPath.row];
[tableView endUpdates];
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
if (indexPath.section==0) {
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
[self.letterAArray insertObject:@"New one" atIndex:indexPath.row];
[tableView endUpdates];
}
}
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
@end
No comments:
Post a Comment