I am using storyboard and custom cells. I am trying to search. It works all fine and loads the custom cell when the tableviewcontroller loads , as soon as i enter a search character, the cell returned is UITableViewCellStyleDefault and i can only set the label, i am not sure why it is not picking the custom cell. Can someone please help.
sorry its a repeat of this but i could not figure out how that worked.
i am using this code.
static NSString *CellIdentifier = #"memberCell";
MemberCell *cell = (MemberCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *item = self.searchList[indexPath.row];
if (cell == nil) {
cell = [[MemberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.memberFullName.text = [item[#"full_name"] uppercaseString] ;
cell.memberPosition.text = [item[#"title"] length] < 1 ? [NSString stringWithFormat:#"%#", item[#"districts"]] : [NSString stringWithFormat:#"%#, %#", item[#"title"], item[#"districts"]];
cell.memberRoom.text = [NSString stringWithFormat:#"Rm %#", item[#"room_number"] ];
[cell.memberImage setImageWithURL:[NSURL URLWithString:item[#"image_thumb"]]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
Deepak
You said "I am trying to search" - do you mean that you added a UISearchDisplayController and you are trying to get your custom cell to appear in that results table view? If so, you have 2 options:
In the tableView:cellForRowAtIndexPath: method you need to dequeue the custom cell from your main table view and NOT the search results table view. When you define a cell in tableview that is in a storyboard, the storyboard only registers it with that tableview, so it isn't dequeueable from any other tableview (I confirmed this with the apple engineer responsible for storyboards at WWDC '13). So, the code would look like this instead:
MemberCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Move your custom cell definition into a standalone xib file (create an empty XIB, drag a UITableViewCell from the objects list and configure it as desired). Define a custom cell class and configure that as the cell class in the xib, and then you can manually register that cell type with both table views using registerNib:forCellReuseIdentifier:
Related
i have a viewcontroller containing multiple tableviews managed by the same viewcontroller.i added search to one of the tableviews... i have added the searchbar amd searchisplaycontroller and wired up the outlets for searchdelegate,searchdatasource,searchbar.etc...
i have the code for filtering the contents of the table working correctly...
below is the code inside the cellforrowatindexpath method
static NSString *simpleTableIdentifier = #"search";
SearchFieldsCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[SearchFieldsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary *field=nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
field = _searchfieldsArray[indexPath.row];
}
else{
field = _fieldsArray[indexPath.row];
}
cell.titleLabel.text=field[#"a"];
cell.subtitleLabel.text=field[#"b"];
cell.detailLabel.text=field[#"c"];
cell.flagImage.image=[UIImage imageNamed:#"ic_flag.png"];
return cell;
but its not displaying the contents of the tableview... even the height of the tableviewcell is set and during the runtime... if i give printobject like po cell.titlelabel.text in the debug console it gives result like
<UILabel: 0xc15d500; frame = (70 0; 241 21); text = 'result'; userInteractionEnabled = NO; layer = <CALayer: 0xc15d5b0>>
i further found out that the line of code
SearchFieldsCell *cell=[tableViewdequeueReusableCellWithIdentifier:simpleTableIdentifier];
is perfectly working ie loading the prototype cells from the storyboard but the line
if (cell == nil) {
cell = [[SearchFieldsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
is returning empty white cells somehow its not loading from the prototype cells from the storyboard... how can this happen...?? anyone please help...
Try to type cast cell with custom cell class. So it will initialize & not be null value.
SearchFieldsCell *cell = (SearchFieldsCell *)[tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];
Hopefully, it will help you.
Thanks.
i know its not done this way...but after so many different ways the solution was to create a custom xib file and make the uisearchresultscontrollers tableview to register this nib file .it worked
[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:#"SearchFieldsCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:#"Search"];
i've got a simple question. How to set up an UITableView with multiple details. I've got plist where i'm storing all the details.
This what I did for just one details.cell.textLabel.text = [[self.array objectAtIndex:indexPath.row] objectForKey:#"name"];
What to do if I want 2 details on my cell?
Many Thanks in advance.
First off you need to set the type of cell you want, if you want a title plus detail you can do this:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
then for setting the title and detail labels, just
[cell textLabel].text = #"some title"
[cell detailTextLabel].text = #"some detail text"
you need to set style for the cell
have look into this guide
If your not happy with using cell.detailTextLabel and one of the provided styles(set up from storyboard) then one option would be to create a custom UITableViewCell.
You can either create a xib and drag and drop a cell in and customize it from there or if your only using 1 table it may be easier to use a prototype cell and customize it directly in your table. The key part is to create a new Class to support your cell.
to the point, i have custom cells, inside it has 2 label and 1 textfield. both label and textfield got input from user. i also have other view that has uitableview inside it. my question is how do i populate cell in uitableview? please help.
this is my code inside tableviewcontroller.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1; // i want to populate this using 'count' but i dont know how.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = _customCell;
_customCell = nil;
}
cell.titleLabel.text = [NSString stringWithFormat:#"%#",titleTextString];
cell.timerLabel.text = [NSString stringWithFormat:#"%#",timerString];
cell.statusLabel.text = [NSString stringWithFormat:#"%#",statusString];
return cell;
}
how do i populate my tableview if i push add button after finishing input by user? Please if you dont mind help me with code. i'm beginner and im hard to understand by using opinion.
If I understood your question correctly, you did a custom nib file for your cells that has 2 UILabel in it and one UITextField, and you want to access these objects when populating your table. Here are some steps for this issue:
First, you have to give a tag number for each object in your custom cell. You find this property in the Attribute Inspector in Interface Builder. Say you gave the first label tag 1, the second label 2 and the text field 3.
Second you have to give a. Identifier for this nib file, for example MyCustomCellIdentifier. This identifier will be used later on in the view that has the table so you can link to it.
Third, also in the custom cell nib, you click on the yellow square that says File's Owner and in the Identity Inspector you change the Class to the class name that has the table that will use this custom cell.
Fourth, in the class that you have the table that will use the custom cell, create an outlet of type UITableViewCell. We will link this in the custom nib cell.
Fifth, goto the custom nib cell, click on the cell window, then in the Connections Inspector link New Referencing Outlet to the File's Owner, you will see the outlet that you created in the table class showing here, simply link to it.
Now since the connections are established thing are more easy, in the cellForRowAtIndexPath function (in the class that contains the table for sure), you have to load the custom cell from the nib file as follows:
static NSString *tableIdentifier = #"MyCustomCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TheNibClassNameOfYourCustomCell" owner:self options:nil];
if([nib count] > 0) cell = theNameOfTheOutletYouUsed;
else NSLog(#"Failed to load from nib file.");
}
Ok, your custom cell is loaded in variable cell, now you have to access every object in it from the tags you created:
UILabel *label1 = (UILabel *)[cell viewWithTag:1];
UILabel *label2 = (UILabel *)[cell viewWithTag:2];
UITextField *textField1 = (UITextField *)[cell viewWithTag:3];
Now you can access everything through label1, label2, and textField1 easily like label1.text = #"Hi";
I hope this answers your question.
In my tableview have custom cells that I initialize from a UITableViewCell class. I have sections for first letters of records and have an indexPath that is being created dynamically.
I wanted to add a search display controller to my tableview. So I did, created all methods to filter data. I am sure that my functions are working well because I am printing array count to screen for search results.
My problem is that the first time view loads, the data is on the screen. But when I hit the search input and type a letter, than I get 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' error. After I used a breakpoint I saw that my custom cell is nil after searching. Data is exist, but cell is not being initialized.
Here is the code I use for custom cell initializing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ObjectCell";
SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *myObject = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.label1.text = [myObject objectForKey:#"myValue"];
return cell;
}
I believe I made a mistake when putting controls in IB. So I added screenshots of objects:
Connections inspector for my table view
Connections inspector for my search display controller
EDIT: Problem is actually solved, I have used a UISearchBar instead of Search Display Controller but I guess this issue remains unsolved. So I'm willing to try any ways to make it work.
As of here search display controller question,
you need to access the self.tableView instead of tableView:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"CellId"];
// do your thing
return cell;
}
For those using iOS 5 and StoryBoards, you would want to use the following method instead of initWithIdentifier:
initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier
Example:
NSString *cellIdentifier = #"ListItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
I'm not sure about how this should work in storeboarding.
But normally you would check if the [tableView dequeueReusableCellWithIdentifier:CellIdentifier] returns a cell.
Because if the cell in not loaded before or there aren't any cells to reuse you will have to create a new cell:
SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SpeakerCell alloc] initWithIdentifier: CellIdentifier];
}
Also when in declaring local variables in Objective-C we tent not to capitalize the first letter.
I had the same issue, with custom cells (built in Storyboard) not being drawn as soon as the first letter was put in the search field. The search was successful however.
Finally I found a good tutorial from Brenna Blackwell suggesting to configure manually the cell drawing in the corresponding subclass of UITableViewCell, adding UILabels and other items.
I am about to go out of my mind here, in my Core Data databse I have a lot of users, i have hooked the database up to a tableviewcontroller via NSFetchedResultController, and when the view loads, I see all my users, and i can perform a push to a detail viewcontroller via storyboard segue... so far so god, my tableview contains a custom cell with an image view and 2 uitextlabels that all has their tag values assigned..
Now, when I perform the search I create a new NSFetchedResultController with a predicate, and performs the performFetch.. I then get the fetchedObjects property and it contains the users that match, so the search request is also working like a charm.. in all my tableviews datasources I check if self.tableView == self.searchdispl.view to see which controller I should query data from.. In all the controllers delegates I check to see which controller is active so i know which view to reload. If i nslog a lot of data it is all fine behind the scenes, the delegate and datasource methods all use the correct view and fetchcontroller..
In my cellForRowAtIndexPath i can log out my user.name, and that is always correct, also when searching.
The issue is that the UISearchDisplayController view that is on top only has empty cells, unless i set my UITableViewControllers dynamic prototype cell to basic, then both tableviews contain that label with the users name ! The segue from the cell still doesn't work, but there is data in the cell.
It seems as the UISearchDisplayControllers view is a generic view, at least when it comes to segues and cell layout...
I have checked all the connections from the UISearchDisplayController to my UITableViewController, and all looks fine...
Why will the SearchDisplayControllers view not accept my cell layout and segue ?
Thank you :)
Update : has just figured out what is going wrong, but not found the solution yet.
In my cellForRowAtIndexPath the cell is always configured as the Default Cell when thr searchdisplayview is in this method.. Do i really need to create my cell in code for the uisearchdisplay to work ? Why can it not use the configured cell from the storyboard ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PersonCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"Default Cell");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSFetchedResultsController *controller = tableView == self.tableView ? self.fetchedResultsController : self.fetchedResultsControllerForSearch;
Person *p;
p = [controller objectAtIndexPath:indexPath];
[(UILabel*) [cell viewWithTag:1] setText:p.name];
[(UILabel*) [cell viewWithTag:2] setText:#"Status"];
if(p.avatarImage){
UIImage *avatar = [[UIImage alloc] initWithData:p.avatarImage];
[(UIImageView *) [cell viewWithTag:3] setImage:avatar];
}
return cell;
}
The issue was that when i got the cell in cellforrowatindexpath, it didn't get it from the original viewcontrollers tableview, so i changed the line from
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
to
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
and now all is good, the cell has the layout from storyboard and segues works like a charm..