I am getting JSONArray data and show phone numbers in table
I am using UISearchBar in UITableView.
When I am type in search bar it reload data and show type value in first cell.
But i clicked first cell means it show old array value.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (isSearching)
{
cell.textLabel.text = [searchResultArray objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [phoneNoArray objectAtIndex:indexPath.row];
}
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[searchResultArray removeAllObjects];
if([searchText length] != 0)
{
isSearching = YES;
[self searchTableList];
}
else
{
isSearching = NO;
}
[self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
tempString = [jsonArray objectAtIndex:indexPath.row];
}
when you set isSearching to yes. Now datasoucre of table has changed. and you have reloaded the data.
and you are supposed to update your datasource methods as well
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isSearching) {
return [searchResultArray count];
}
else
{
return [phoneNoArray count];
}
}
HTH
Related
Hi everyone I am a newbie to iOS.
I have implemented a UITableView with 3 sections in UITableViewController. And I have displayed the section rows to each section.
- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
sectionTittle=[[NSMutableArray alloc]initWithObjects:#"Real Madrid",#"Barcelona",#"Liverpool", nil];
firstSection=[[NSMutableArray alloc]initWithObjects:#"Cr7",#"Garath Bale",#"Pepe",
nil];
secondSection=[[NSMutableArray alloc]initWithObjects:#"Lionel Messi",#"Neymar",#"Pique",
nil];
thirdSection=[[NSMutableArray alloc]initWithObjects:#"Gerrard",#"Saurez",#"Lallana",
nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionTittle.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section==0)
{
return [firstSection count];
}
if(section==1)
{
return [secondSection count];
}
if(section==2)
{
return [thirdSection count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"hi" forIndexPath:indexPath];
cell.textLabel.textColor=[UIColor brownColor];
if (indexPath.section == 0)
{
cell.textLabel.text=[firstSection objectAtIndex:indexPath.row];
}
if (indexPath.section == 1)
{
cell.textLabel.text = [secondSection objectAtIndex:indexPath.row];
}
if (indexPath.section == 2)
{
cell.textLabel.text = [thirdSection objectAtIndex:indexPath.row];
}
// Configure the cell...
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sectionTittle objectAtIndex:section];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
Now my output looks like this:
I want to move the rows between the these 3 sections. How can I do it..? While I try doing it the row moves between the section, but while saving it creates a problem.
How can i go through it..?
Thanks
You need to utilise UITableView re-ordering capacity. The cells can be re-ordered across sections.
To enable re-ordering mode:
[self.tableView setEditing:TRUE animated:TRUE];
This enables the TableView in editing mode which includes enables deletion mode along with re-ordering. If you do not want the deletion mode implement:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
The re-order support accessory view is shown if you have implemented moveRowAtIndexPath like, for your case:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// Logic to change data source
NSMutableArray *deleteFrom = [self getDataSourceArrayFromIndexPath:sourceIndexPath];
NSMutableArray *addTo = [self getDataSourceArrayFromIndexPath:destinationIndexPath];
[addTo insertObject:[deleteFrom objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
[deleteFrom removeObjectAtIndex:sourceIndexPath.row];
}
where getDataSourceArrayFromIndexPath is:
-(NSMutableArray*) getDataSourceArrayFromIndexPath:(NSIndexPath*) index
{
switch (index.section) {
case 0:
return self.firstSection;
break;
case 1:
return self.secondSection;
break;
case 2:
return self.thirdSection;
break;
default:
break;
}
return nil;
}
Use This Code
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSMutableArray *arrayTemp=[arrayMain objectAtIndex:sourceIndexPath.row];
[arrayVisibleCountryList removeObjectAtIndex:sourceIndexPath.row];
[arrayVisibleCountryList insertObject:arrayTemp atIndex:destinationIndexPath.row];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
//Uncomment these lines to enable moving a row just within it's current section
if ([sourceIndexPath section] != [proposedDestinationIndexPath section])
{
proposedDestinationIndexPath = sourceIndexPath;
}
return proposedDestinationIndexPath;
}
You can use below tableView method. It moves your cell from sourceIndexPath to destinationIndexPath.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
I'm new to iOS. I'm using storyboard for UI development. I created a custom header cell with a label and activity Indicator. I created two section in my table view.In first section there is one row and in that row there is a switch in the accessory view.Now is want to do when I on/off the switch the activity indicator start/stop animating.But this is not happening in my case. please help me.
Here is the code snippet:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"bluetoothCell"];
UISwitch *addswitch=nil;
if (addswitch==nil)
{
addswitch=[[UISwitch alloc]initWithFrame:(CGRectZero)];
}
if (indexPath.section==0)
{
if (indexPath.row==0)
{
cell.accessoryView=addswitch;
[addswitch addTarget:self action:#selector(bluetoohSwitchClicked:) forControlEvents:UIControlEventValueChanged ];
cell.textLabel.text=#"Bluetooth";
}
}
else if (indexPath.section==1) {
cell.textLabel.text=#"bluetooth devices";
cell.accessoryView=nil;
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section
{
HeaderTableViewCell *customheader;
NSLog(#"section.. %ld",(long)section);
customheader=[ tableView dequeueReusableCellWithIdentifier:#"headercell"];
self.customheader=customheader;
customheader.autoresizesSubviews=YES;
if (section==0) {
customheader.activityloader2.hidden=NO;
customheader.tag=0;
NSLog(#"section %ld,%#",(long)section,customheader);
}
else if(section==1) {
customheader.activityloader2.hidden=NO;
customheader.tag=1;
NSLog(#"sections %ld,%#",(long)section,customheader);
}
return customheader;
}
-(void)bluetoohSwitchClicked:(id) sender
{
UISwitch* switchControl = sender;
if ([switchControl isOn])
{
self.onoffswitch=#"ON";
[switchControl setOn:YES animated:YES];
}
else
{
self.onoffswitch=#"OFF";
[switchControl setOn:NO animated:YES];
}
[self bluetoothStatechanged:[self.onoffswitch isEqualToString:#"ON"]? YES:NO];
}
-(void)bluetoothStatechanged:(BOOL)on
{
HeaderTableViewCell* customheader=(HeaderTableViewCell *)[self tableView:self.maintableview2 viewForHeaderInSection:1];
__block CGRect frame=customheader.frame;
if (on==YES) {
[customheader.activityloader2 startAnimating];
NSLog(#"yes block");
}
else
{
[customheader.activityloader2 stopAnimating];
NSLog(#"No block");
}
}
I have created a stepper in a CustomCell of a UITableView. However, I would like the stepper to only be visible when a particular row is selected. To this end, I tried the following:
In tableView:cellForRowAtIndexPath:
cell.customStepper.hidden=NO;
and in tableView:didSelectRowAtIndexPath:
cell.customStepper.hidden=YES;
But the stepper is still hidden. What am I missing?
#interface BRNCategoryViewController ()
{
NSMutableArray *arySelectCategory;
NSMutableArray *aryCategory;
}
- (void) viewDidLoad
{
arySelectCategory=[NSMutableArray new];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return aryCategory.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BRNCategoryCell *cell=[[BRNCategoryCell alloc]initWithOwner:self];
if ([arySelectCategory containsObject:[aryCategory objectAtIndex:indexPath.row]])
{
cell.customStepper.hidden = NO;
}
else
{
cell.customStepper.hidden = YES;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([arySelectCategory containsObject:[aryCategory objectAtIndex:indexPath.row]])
{
[arySelectCategory removeObject:[aryCategory objectAtIndex:indexPath.row]];
}
else
{
[arySelectCategory addObject:[aryCategory objectAtIndex:indexPath.row]];
}
[tblEventCategory reloadData];
}
In cellForRowAtIndexPath
cell.customStepper.hidden=YES;
And
In didSelectRowAtIndexPath
VideosCell *cell = (VideosCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.customStepper.hidden = NO;
Actually i have stored too many data in table view and now I want to found my data easily so I need a search bar. Can any one give me a little solution?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifire = #"CellIdentifire";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
Follow these steps:
Drag and drop search bar on table view header
Set delegate for search bar to your view controller
Use following code to search:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//Remove all objects first.
[self searchTerm:searchText];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Cancel clicked");
searchBar.text = #"";
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO];
[self showAllData];// to show all data
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Search Clicked");
[searchBar setShowsCancelButton:NO];
[searchBar resignFirstResponder];
[self searchTerm:searchBar.text];
}
-(void)showAllData {
//load all data in _tableViewDataSourceArray, and reload table
}
-(void)searchTerm : (NSString*)searchText
{
if (searchText == nil) return;
_tableViewDataSourceArray = //YOUR SEARCH LOGIC
[self.tableView reloadData];
}
Edit:
In case you wanna create Search bar with code then, Initialize a search bar and pass it as headerView Of your table view, inside
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Dont's forget to return height of view in
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
So I was trying to combine the search bar with the Nav bar for tableview in iOS7.
I called
self.searchDisplayController.displaysSearchBarInNavigationBar = YES; And it looks fine.
However, when I tap anywhere in the tableview, the search bar is activated. There's essentially no way I can regularly click a table cell.
I'm wondering what exactly did I miss here?
And for reference the code related to tableview looks like this: (I used storyboard to handle click logic)
#pragma mark - Table view delegate method
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
} else {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [self.contacts[section] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"contactCell";
UITableViewCell *cell;
Contact *contact;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
contact = searchResults[indexPath.row];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
contact = [[self.contacts objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:7];
UIImageView *starMark = (UIImageView *)[cell viewWithTag:9];
NSString *nameString = [NSString stringWithFormat:#"%# %#",contact.firstName, contact.lastName];
if ([contact.star isEqual:#0]) {
starMark.hidden = YES;
} else {
starMark.hidden = NO;
}
[nameLabel setText:nameString];
return cell;
}
And the segue method is defined here:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"pushContactDetail"]) {
NSIndexPath *indexPath;
Contact *contact;
if (self.searchDisplayController.active == YES) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
contact = searchResults[indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
contact = [[self.contacts objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
//get indexPath from selected sushi
//initialize the detail view controller and push it
CIContactViewController *destViewController = segue.destinationViewController;
destViewController.contact = contact;
}
}