Search bar for table view - ios

I want to have 2 tables in one view controller and to make a search only in one of them. How can i do ?
I know how to implement search bar , but I don't know how to make a difference between tables. I was trying to do this with tags, but is not working.
Any suggestion will be helpful.
UITableViewCell *cell ;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
if (tableView.tag==1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = self.tableData[indexPath.row];
}}
else if (tableView.tag==0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"cell2"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [historique objectAtIndex:indexPath.row];
}
return cell;
}
i was trying also this :
and even if i put like this
self.tableSource= self.searchDisplayController.searchResultsTableView; the result is the same

Use variables to handle the two tables.
Then you can perform actions only on the one you want.
Exemple :
#interface MyClass
#property (nonatomic, strong) UITableView *searchableTableView;
#property (nonatomic, strong) UITableView *nonSearchableTableView;
#end

You need to create two properties for both table view.
#property (strong, nonatomic) IBOutlet UITableView *table1;
#property (strong, nonatomic) IBOutlet UITableView *table2;
Then implement delegate methods of table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([tableView isEqual:table1])
{
return YOUR_VALUE;
}
else
{
return YOUR_VALUE;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableView isEqual:table1])
{
return [array1 count]
}
else
{
return [array2 count];
}
Same way implement the cellForRowAtIndexPath method of delegate.

Related

How to give action to tableview row and how to add Add disclosure indicator in it?

Here is description of my class.
At first refer the above image.
So here, I created a button.(see in scene 1 in Image) After clicking a button one table view will appear (see in scene 2 in Image). If I click any cell then that table view cell value display on that button text (see in scene 3 in Image). Here I not given any action to any table view cell.
Now, I given action to these row(only to index 0 for testing). When I click uitableviewcell on Uitableview 1 then it will show another tableview 2 and previous table view hide. see following image.
So in this tableview 2 I want to give action i.e when i click on any row from tableview row of second table for example :Suppose I clicked on Arjun then button label need to change by Arjun and this tableview have to hide or If click on tableview row like Karan then on one pop up I want to display like Karan selected.
In short, I want to know how to give action to that second tableview row and how to add disclosure indicator to first table view.
ViewCotroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (weak, nonatomic) IBOutlet UITableView *tableview1;
#property (weak, nonatomic) IBOutlet UITableView *tableview2;
#property(strong,nonatomic)NSArray *arr1;
#property(strong,nonatomic)NSArray *arr2;
- (IBAction)buttonAction:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arr1=[[NSArray alloc]initWithObjects:#"One",#"Two",#"Three",#"Four",#"Five", nil];
self.arr2=[[NSArray alloc]initWithObjects:#"Arjun",#"Karan",#"Amar", nil];
self.tableview1.delegate=self;
self.tableview1.dataSource=self;
self.tableview2.delegate=self;
self.tableview2.dataSource=self;
self.tableview1.hidden=YES;
self.tableview2.hidden=YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows;
if(tableView == _tableview1) rows = [_arr1 count];
if(tableView == _tableview2) rows = [_arr2 count];
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simple=#"SampleIndentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simple];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simple];
}
// cell.textLabel.text=[self.arr1 objectAtIndex:indexPath.row];
if(tableView == _tableview1)
cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];
if(tableView == _tableview2)
cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= [self.tableview1 cellForRowAtIndexPath:indexPath];
[self.button setTitle:cell.textLabel.text forState:UIControlStateNormal];
self.tableview1.hidden=YES;
if(tableView == _tableview1)
{
if(indexPath.row==0)
{
self.tableview1.hidden=YES;
self.tableview2.hidden=NO;
if(indexPath.row==0)
{
// cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
// cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
//cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];
//self.tableview2.hidden=YES;
}
if(tableView == _tableview2)
cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
- (IBAction)buttonAction:(id)sender {
if(self.tableview1.hidden==YES)
{
self.tableview1.hidden=NO;
}
else
{
self.tableview1.hidden=YES;
}
}
#end
check following answer..!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= [self.tableview1 cellForRowAtIndexPath:indexPath];
[self.button setTitle:cell.textLabel.text forState:UIControlStateNormal];
self.tableview1.hidden=YES;
if(tableView == _tableview1)
{
if(indexPath.row==0)
{
self.tableview1.hidden=YES;
self.tableview2.hidden=NO;
if(indexPath.row==0)
{
// cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
// cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}
//cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];
//self.tableview2.hidden=YES;
}
if(tableView == _tableview2)
cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
}

Values for uilabels of custom tableview cell is not being set

Im using a tableview with custom cells.
Following is my cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// AssigneeCell *cell = (AssigneeCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
static NSString *cellIdentifier = #"Cell";
AssigneeCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:#"AssigneewiseView" bundle:nil] forCellReuseIdentifier:#"Cell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
NSLog(#"call");
}
cell.unitNameVal = unitNameResult[indexPath.row];
cell.domainNameVal = domainNameResult[indexPath.row];
cell.compliedVal = compliedResult[indexPath.row];
cell.delayedVal = delayedResult[indexPath.row];
cell.inProgressVal = inProgressResult[indexPath.row];
cell.notCompliedVal = notCompliedResult[indexPath.row];
cell.totalVal = totalValResult[indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [compliedResult count];
}
numberOfRowsInSection is called and hence my tableview contains multiple views depending upon the count of compliedResult.
But the problem is that, the values for uilabels present in my custom cell is not being assigned and hence its not visible. Why is that so?
First suggestion, create your model:
#interface Result: NSObject
#property (nonatomic) NSString* unitName;
#property (nonatomic) NSString* domainName;
#property (nonatomic) NSString* complied;
#property (nonatomic) NSString* delayed;
#property (nonatomic) NSString* inProgress;
#property (nonatomic) NSString* notComplied;
#property (nonatomic) NSString* totalVal;
#end
In this way, your viewController will have an array of Result object.
Inside your custom cell, define:
#implementation AssigneeCell
+ (NSString*)cellIdentifier {
return #"Cell";
}
- (void)setupWithResult:(Result*)result {
self.unitNameVal.text = result.unitName;
self.domainNameVal.text = result.domainName;
self.compliedVal.text = result.complied;
self.delayedVal.text = result.delayed;
self.inProgressVal.text = result.inProgress;
self.notCompliedVal.text = result.notComplied;
self.totalVal.text = result.totalVal;
}
#end
Finally in your view controller:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView registerNib:[UINib nibWithNibName:#"AssigneewiseView" bundle:nil] forCellReuseIdentifier:[AssigneeCell cellIdentifier]];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString* cellIdentifier = [AssigneeCell cellIdentifier];
AssigneeCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cell setupWithResult: results[indexPath.row]];
return cell;
}
This ensure clean code and code reuse.
Since unitNameVal, domainNameVal, etc. are labels, make sure you are setting their text property. What you are doing right now is setting the labels themselves as different objects.
Assuming your arrays are arrays of strings, this should do the trick for you:
cell.unitNameVal.text = unitNameResult[indexPath.row];
cell.domainNameVal.text = domainNameResult[indexPath.row];
...

Custom Cells are created, but not displayed

So I've used this tutorial to populate a UITableView with custom cells that represent balances. When stepping through the code, I witness the correct amount of cells get created (only 4 with the current test data) and their labels' text set correspondingly.
My problem is when the table is displayed on the screen, only the first row/cell is displayed.
Any insight as to why this could be occurring would be greatly appreciated!
Removed old code.
BalanceCell.h:
#import <UIKit/UIKit.h>
#interface BalanceCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
#property (weak, nonatomic) IBOutlet UILabel *amountLabel;
#property (weak, nonatomic) IBOutlet UILabel *modifiedLabel;
#end
EDIT:
My TableView delegate methods are now as follows:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [_balances count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
BalanceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[BalanceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [_hex colorWithHexString:_themeColourString];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(BalanceCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
Balance *item = [_balances objectAtIndex:indexPath.row];
cell.nameLabel.textColor = _themeColour;
cell.nameLabel.text = item.name;
cell.amountLabel.textColor = _themeColour;
cell.amountLabel.text = [NSString stringWithFormat:#"%#%#", item.symbol, item.value];
cell.modifiedLabel.textColor = _themeColour;
cell.modifiedLabel.text = [NSString stringWithFormat:#"%#", item.modified];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 94;
}
As #Sebyddd suggested, I now register the NIB in the viewDidLoad method.
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"BalanceCell" bundle:nil] forCellReuseIdentifier:#"Cell"];
}
These changes may make my code more correct but still only the first cell is displayed.
If cells are getting created and returned properly I guess height is not being set propery. By default I beleive all cells have a height of 44. If your cell exceeds this height it might not get displayed.
You can tell the tableview to adjust height for every cell using (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath delegate
In that delegate just return your cells height.
EDIT:
You are using dequeueReusableCellWithIdentifier: which will return A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.
Instead use dequeueReusableCellWithIdentifier:forIndexPath: which will return A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.
You need to register the nib/class for that custom cell in viewDidLoad
Try this:
if (cell == nil) {
[tableView registerNib:[UINib nibWithNibName:#"BalanceCell" bundle:nil] forCellReuseIdentifier:#"Cell"];
cell = [[BalanceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
Use this tuto : http://www.appcoda.com/uitableview-tutorial-storyboard-xcode5/ , your tuto is a bit outdated, and hard to follow !

UITableView crashing when selecting row

When I click on a cell in my tableview, the app crashes with:
// QuestionViewController.h
#interface QuestionViewController : UIViewController <UITableViewDelegate , UITableViewDataSource> {
}
#property (nonatomic, strong) AppDelegate *app;
#property (nonatomic, retain) PFObject *feed;
#end
// QuestionViewController.m
#synthesize app, feed;
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [[feed objectForKey:#"options"] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *cellTxt = [[[feed objectForKey:#"options"] objectAtIndex:indexPath.row] objectForKey:#"option_text"];
[[cell textLabel] setText:cellTxt];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"clicked cell");
}
- (void)viewDidLoad {
[super viewDidLoad];
app = [[UIApplication sharedApplication]delegate];
feed = [app.feed objectAtIndex:0];
}
I have implemented didSelectRowAtIndexPath, but it doesn't get called before crashing.
Other threads on SO suggest that I have unconnected outlets but I have checked that this isn't the case.
I am creating multiple instances of the above UIViewController like this:
for (int a = 0; a < totalQuestions; a++) {
QuestionViewController *temp = (QuestionViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"aQuestion"];
temp.view.frame = CGRectMake(self.view.frame.size.width*a+scrollWidthBeforeAppend, 0, 320, 443);
[scroller addSubview:temp.view];
}
And adding them to a scroll view. They display correctly, the UITableView is populated, and everything seems to work fine other than when I try and click on a cell. Any suggestions?
Your temp UIViewControllers get deallocated by the time you press a cell.
You should keep a reference to them to prevent this, for example in an array.

How can i use custom UITableViewCell and UITableView in same xib

I want to use a custom UITableviewCell with UITableView in same xib without creating a UITableViewCell class?
As you can see bellow i set the identifier for UITableViewCell and used it like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
But Not Working?
Add a UITableViewCell *customCell property to your view controller (for example, your file ShowUsers.h)...
#property (nonatomic, strong) IBOutlet UITableViewCell *customCell;
and connect it to the custom cell in your xib. Then use the following code in cellForRow (for example, your file ShowUsers.m) :
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NibName" owner:self options:nil];
cell = self.customCell;
self.customCell = nil;
}
Yes you can do as following code
Under ViewDidLoad or ViewWillAppear
label1Array=[NSMutableArray arrayWithObjects:#"A",#"B",nil];
label2Array=[NSMutableArray arrayWithObjects:#"C",#"D",nil];
label3Array=[NSMutableArray arrayWithObjects:#"E",#"F",nil];
UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"aCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *label1=[[UILabel alloc]init];
label1.frame=CGRectMake(0,0,40,40);
label1.text=[label1Array objectAtIndex:indexPath.row];
................
[cell.contentView addSubView: label1];
UILabel *label2=[[UILabel alloc]init];
label2.frame=CGRectMake(50,0,40,40);
label2.text=[label2Array objectAtIndex:indexPath.row];
[cell.contentView addSubView: label2];
UILabel *label3=[[UILabel alloc]init];
label3.frame=CGRectMake(100,0,40,40);
label3.text=[label3Array objectAtIndex:indexPath.row];
[cell.contentView addSubView: label3];
}
Hope this Helps !!!
See..You can do like this. Here you are adding two UITableViewCell in one TableView using XIB.
In Class.h file :-
Declare one mutable array for number of cells.
{
NSMutableArray * sectionRows;
}
#property (nonatomic,retain) IBOutlet UITableViewCell *addTvc1;
#property (nonatomic,retain) IBOutlet UITableViewCell *addTvc2;
In Class.m :-
#synthesize addTvc1, addTvc2;
- (void)viewDidLoad
{
[super viewDidLoad];
sectionRows = [[NSMutableArray alloc] init];
[sectionRows addObject:[[NSMutableArray alloc]
initWithObjects:addTvc1, nil]];
[sectionRows addObject:[[NSMutableArray alloc]
initWithObjects:addTvc2, nil]];
}
In UITableViewDelegate Method -
Add This -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [sectionRows count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
return [[sectionRows objectAtIndex:section] count];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
return [[sectionRows objectAtIndex:indexPath.section]
objectAtIndex:indexPath.row];
}
Along with code, In Class's XIB drop and drag two UITableViewCell which should be outside from the view and add those cells with Files Owner.
It will work perfectly. Here no need to create one separate custom UITableViewCell class.

Resources