Style UITableViewCell inside UIScrollView/UIView - ios

Overview
I've added a UITableView inside a UIScrollView using the storyboard.
My simple goal is to style the UITableViewCell's inside this table view.
Code
Currently, I have added a UITableView property to the view controller and added the outlet in the storyboard.
#interface NWDetailViewController : UIViewController<UITableViewDelegate,
UITableViewDataSource> {
IBOutlet UIScrollView *scrollView;
...
IBOutlet UITableView *tableView;
IBOutlet NSLayoutConstraint *tableViewHeightConstraint;
}
Created a custom UITableViewCell (NWLocationTableViewCell), added a property and connected the outlet in the storyboard:
#interface NWLocationTableViewCell : UITableViewCell {
IBOutlet UILabel *titleLabel;
}
And this is how i'm trying to populate the cell in my Controller:
-(UITableViewCell *)tableView:(UITableView *)thisTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"LocationCell";
// Same with both tableView and thisTableView
NWLocationTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[NWLocationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
[cell.titleLabel setText:#"test"];
// or
UILabel *label = (UILabel *)[cell.contentView viewWithTag:2112];
[label setText:#"test"];
NSLog(#"%#, %#", cell.contentView.subviews, cell.class);
return cell;
}
But both methods yield nothing, hence I can't style it. Adding any static objects (UIButton for example) also don't show up.
The logs show: "(), NWLocationTableViewCell"

Its a naming problem.
in your method header you pass thisTableView as the tableView to use, but then you call [tableView dequeueReusableCellWithIdentifier]; to get a cell. It should generate an error, but probably there is another variable by that name, or a property? Anyways, if thats your verbatim code, theres your error.

Related

iOS 7: Custom Style Cell in Tableview: How to reference labels?

I was following a simple iOS 7 tutorial and using the standard subtitle cell type in a tableview, and got two labels for the Title and Subtitle. The labels were: textLabel and detailTextLabel
I now want to customize the cells in my tableView, so I switched the style to Custom, and created two labels on the storyboard. I then linked IBOutlets to those labels in the interface section of my .m file. It looks something like this:
#interface MasterViewController ()
#property (weak, nonatomic) IBOutlet UILabel *textLabel;
#property (weak, nonatomic) IBOutlet UILabel *detailTextLabel;
#end
This is what my code to render my cells looks like, it is fairly standard, I think.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"KeyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *key = [items objectAtIndex:indexPath.row];
NSString *name = [key objectForKey:#"name"];
NSString *type = [key objectForKey:#"type"];
cell.textLabel.text = name;
cell.detailTextLabel.text = [NSString stringWithFormat:#"Type : %#", type];
return cell;
}
Am I doing this wrong?
By creating an IBOutlet in your VC (your delegate) you have tried to link a single instance of those UILabels.
Create a new UITableViewCell subclass and set your prototype cells to that class in IB. Then drag your outlets across to that subclass. This is the only way using outlets that will work.
Unfortunately I am a bit rushed for time otherwise I would post a full answer but here's how to do it:
1.Use File->New->File...;
2. Select Cocoa Touch and then Objective-C Class;
3. Enter Class Name and make sure the Subclass is of type UITableViewCell;
4. Click Next and create the classes in your project folder of choice.
5. Go to your storyboard, click on your prototype cell;
6. In the Utilities (right-hand) pane, click on the Identity Inspector (3rd icon) and in the class entry at the top enter the name of your new class.
7. Drag outlets to the .m file as you did before!
As an aside, using Storyboards and Prototype cells means that your if (cell == nil) { ...} section is now redundant and can be removed as storyboards are guaranteed to return a cell.
You should give names to custom labels different from the standard.
Also, you need to subclass UITableViewCell and use it in cellForRowAtIndexPath method.

Custom Prototype Cells - Storyboard Compilation Error

I am creating custom prototype cells in my program, but I am unable to get the program to compile.
Here's what it looks like (i've kept it fairly standard right now, just to get it working):
I have also created a new custom class homeTable, here's what homeTable.h looks like:
#interface homeTable : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *itemName;
#property (weak, nonatomic) IBOutlet UILabel *itemType;
#end
I've set the tableViewCell to have a the custom class homeTable.
And here is what the code in my masterViewController looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"KeyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *key = [ownedItems objectAtIndex:indexPath.row];
NSString *name = [key objectForKey:#"name"];
NSString *type = [key objectForKey:#"type"];
cell.itemName.text = name;
cell.itemType.text = type;
return cell;
}
When I try running the program it throws up a Interface Builder Storyboard Compilation failed error.
I've been stuck on this since yesterday, What am I doing wrong?
I may have miss a bigger issue but are here a few things that you could have done wrong.
First make sure your custom class and the cell identifier are correctly set-up in your storyboard.
Also right click on the tableViewController in the left panel inside the storyboard to take a look at your IB connections. You may have linked something you deleted later. You may see links with a ! telling you somethings isn't right.
Then you could change things in :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
First with storyboard no need for :
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Since cell will never be nil. Storyboard takes care of it, put a breakpoint there you'll see.
Also you are trying to access your custom properties
#property (weak, nonatomic) IBOutlet UILabel *itemName;
#property (weak, nonatomic) IBOutlet UILabel *itemType;
Without specifying the matching class, I would recommend you use your custom tableViewCell class as follow :
homeTable *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
By the way the name of your class should respect naming conventions. (But that will not help with your issue, just good practices).
If you still have a problem, I would recommend you post more details about what's going on in your storyboard.

UISearchBar with UITableView containing custom cells => blank cells

I have a UITableView containing custom cells. All works fine. But after, I decided to add a searchBar in order to... Search !
So, I added a "Search Bar and Search Display Controller" from the "Object Library" in my xib file, under the UITableView.
I created a specific class for the custom cell :
"CustomCell.h"
#class CustomCell;
#interface CustomCell : UITableViewCell
{
}
#property (weak, nonatomic) IBOutlet UILabel *lblDate;
#property (weak, nonatomic) IBOutlet UILabel *lblAuteur;
#end
"CustomCell.m"
no interesting stuff
"CustomCell.xib"
The "Event" class :
#interface Event : NSObject
{
}
#property (strong, nonatomic) NSString *desc;
#property (strong, nonatomic) NSString *dateCreation;
#end
And the view containing the UITableView and the UISearchBar :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
// Determinate the current event
Event *currentEvent;
if (tableView == self.searchDisplayController.searchResultsTableView)
currentEvent = [filteredArray objectAtIndex:indexPath.row];
else
currentEvent = [notFilteredArray objectAtIndex:indexPath.row];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.lblAuteur.text = [currentEvenement desc];
cell.lblDate.text = [currentEvenement dateCreation];
return cell;
}
Ok, now we can come to my problem. After loading the tableView, the custom cells are displaying well. But not if I use the SearchBar :
If there is an event with the desc attribute equal to "foo", and if I enter "bar" in the SearchBar, I obtain the "No results" message. It's normal.
If there is an event with the desc attribute equal to "foo", and if I enter "foo" in the SearchBar, the cells are displaying, but without their content ! I'm just seeing the cells' borders, and the lblDate and the lblAuteur are equal to nil.
Why have I this behaviour ? Thanks a lot for your help... I precise that the filteredArray and the notFilteredArray are correctly filled (I checked that many times). It means that the search mechanism is working well.
After much "searching" (pun) I've found the problem.
When you reference the tableview with the prototype cell you can't rely on the tableview that is passed in because sometimes that tableview is the search tableview without the prototype cell.
so instead of...
HomeTabCell *cell = (HomeTabCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
replace the "tableview" above with a direct connection to the table view with the prototype cell
HomeTabCell *cell = (HomeTabCell *)[self.HomeTableView dequeueReusableCellWithIdentifier:CellIdentifier];
Hope this helps!
Check these settings, if there not
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
try this,
Sorry for people searching to resolve this issue...
I could not fix it, and the prime contractor do not want this function anymore, so I gave up.

UITableViewCell with xib

I'm developing an app for iOS which use the xib files.
Usually I use Storyboard and I don't know how to set up a UITableViewCell with xib files. When I make a xib file with a UITableView, I see a table with some row, now I need to edit this row to write what I stored in an array.
How can I use the xib file to design a UITableViewCell?
I need to do a very simple table: I would to use the Basic preset for the cell to display a title.
I know that I've to connect the delegate and DataSource to the file owner for table view and I put in the file owner the UITableViewDelegate and UITableViewDataSource.
Now how I can edit the content of the cell?
I found on the web some guide that tells to create a xib file with a UITableViewCell and I did it, but I don't know how to work with this
Firstly, you need to create the class for the customCell which inherits from UITableViewCell.
Now, add the properties you want to have in your customCell. In this example I added cellImage and cellLabel.
#property (nonatomic, strong) IBOutlet UILabel *cellLabel;
#property (nonatomic, strong) IBOutlet UIImageView *cellImageView;
After that you need to link the UILabel and the UIImageView from the CustomCell to the Nib.
You need to add:
- (void)viewDidLoad
{
....
[self.tableView registerNib:[UINib nibWithNibName:#"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
.....
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCellReuse";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
[cell.cellImageView setImage:[UIImage imageNamed:#"whatever"]];
[cell.cellLabel setText = #"whatever"];
return cell;
}

Data is disappearing from UITableView

I am building an iOS app with a custom tab bar at the top. When i click on the home icon, I want it to show a tableview directly below the tab bar. Currently my code does just that, however the data doesnt show correctly. If I limit my Cells to 4 or less (in the numberFoRowsInSection), the data will show, if i have like say 15 cells, the data shows for a split second and then it disappears. I have been looking at tutorials all over the place and everything seems to be correct, the data will show correctly in a stand alone view(like i created a similar to a singleview application in the story board and made that the initial view). I dont know where i am wrong. Below is my code in implementation file of the tableviewContoller class:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 4;//this works... but change to 15 it doesnt work
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PrayerCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (cell == nil) {
NSLog(#"cell is Nil"); //never hits this
cell = [[PrayerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CustomCell"];
}
//the data showing is the correct data that i want to see
cell.dName.text = [[news objectAtIndex:indexPath.row] objectForKey:#"displayname"];
cell.priority.text = [[news objectAtIndex:indexPath.row]objectForKey:#"priority"];
cell.dateTime.text = [[news objectAtIndex:indexPath.row] objectForKey:#"datetime"];
cell.numPraying.text = #"2 praying";
cell.requestPreview.text = [[news objectAtIndex:indexPath.row] objectForKey:#"request"];
NSLog(#"created cell") //verify cell was made
return cell;
}
Here is my PrayerCell.H file
#interface PrayerCell : UITableViewCell
#property (weak, nonatomic)IBOutlet UILabel *requestPreview;
#property (weak, nonatomic)IBOutlet UILabel *dName;
#property (weak, nonatomic)IBOutlet UILabel *dateTime;
#property (weak, nonatomic)IBOutlet UILabel *numPraying;
#property (weak, nonatomic)IBOutlet UILabel *priority;
#end
I made no Changes to the prayerCell.M file
if you need me to post any other code blocks/screenshots please let me know.
The cell should not be an ivar, it should be a local variable. You also need to dequeue the cell in your tableView:cellForRowAtIndexPath: method:
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];

Resources