Custom UITableViewCell Created in StoryBoard Results in duplicated UILabels - ios

I have been through many questions on SO before asking this.
I have an issue where i have created a custom UITableView cell in a StoryBoard. I have subclassed UITableViewCell and exposed properties to link my various components to within the cell.
I am NOT creating or adding any components to the cell in
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I simply use the
[tableView dequeueReusableCellWithIdentifier:cleanseCell forIndexPath: indexPath];
call to reuse cells. I do some customisation on the cells but I don't add anything new. I just modify an image, some labels and a text field . The cell reuse identifier is set correctly both in the code and on the component in the storyboard.
The problem is that when the cell is reused, somewhere under the hood the UILabels are being duplicated. I have a UITextField in there as well - it doesn't have this problem. Only the UILabels are somehow duplicated.
So the cell presents fine the first time with the correct information. Then the next time the cell is created its still fine. The third time the cell shows up with Overlapping UILabels. One with the new text and one with the text of the original cell. At any rate there are two UILabels in the cell now where there was only one before and I didn't add it there.
Anyone else experienced this or have some comment to make?
EDIT:
This is my UITableViewCell - There is nothing in the implementation other than synthesising the properties (which is not even required anyway)
#import <UIKit/UIKit.h>
#interface SJCleanseNotificationCell : UITableViewCell
#property (nonatomic)float openHeight;
#property (nonatomic, strong)IBOutlet UIImageView *iconView;
#property (nonatomic, strong)IBOutlet UILabel *dateTimeLabel;
#property (nonatomic, strong)IBOutlet UILabel *titleLabel;
#property (nonatomic, strong)IBOutlet UITextView *messageLabel;
-(IBAction)dismiss:(id)sender;
-(IBAction)activate:(id)sender;
#end
And this is the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cleanseCell = #"CleanseCell";
NSDictionary *cleanse = sjTimer.cleanseNotification;
SJCleanseNotificationCell *cell;
if([_tableView respondsToSelector:#selector(dequeueReusableCellWithIdentifier:forIndexPath:)])
cell = (SJCleanseNotificationCell*)[_tableView dequeueReusableCellWithIdentifier:cleanseCell forIndexPath: indexPath];
else
cell = (SJCleanseNotificationCell*)[_tableView dequeueReusableCellWithIdentifier:cleanseCell];
[cell.dateTimeLabel setText:[cleanse objectForKey:#"date"]];
[cell.titleLabel setText:[cleanse objectForKey:#"title"]];
[cell.messageLabel setText:[cleanse objectForKey:#"message"]];
NSNumber *stage = (NSNumber*)[cleanse objectForKey:#"stage"];
if(stage)
[cell.iconView setImage:[[SJCleanseTimer instance]bottleImageForCleanseStage:stage.integerValue]];
cell.delegate = self;
cell.openHeight = 100;
return cell;
}

The problem was I had Clears Graphics Context unchecked in the inspector on the UILabel elements. After checking that checkbox it behaved normally.

Related

UITableview buttons getting highlighted on scroll

I have toggle buttons in my tableview cells and I click them on for some cells but when I scroll down, those same buttons are selected for the bottom cells as well even though I didn't select them yet. I know this is happening because of the tableview reusing cells...is there any way I can fix this?
The cells are dynamic, not static.
what the tableview looks like
** EDIT: Also, lemme know if my logic seems alright: I tried creating a mutable array in my viewcontroller class and then setting all it's values to #"0". Then, in my tableviewcell's class, I set the value in the array to #"1" at the index of the current cell if I select the button, so then back in my viewcontroller class, I can tell if I have already selected a button at that cell or not. The only flaw is that I can't access the array in my tableviewcell's class, it is coming out at null...i guess that it because of the mvc pattern in objective c. Any advice?
EDIT
I am still unable to resolve my issue. Can someone please help me? I have been stuck on it for a while now!
I am trying to create a tableview where the cells have a check and cross button and when I click the check button, it should turn green, but the same button in other cells should remain gray, however, when I scroll down, some cells that I didn't select buttons in still turn green...because of cell recycling.
I am using delegates and protocols right now but it isn't working; perhaps I am using it wrong?
I am setting yesChecked value in IBaction functions in my cell class, and in my viewcontroller class, I am using that yesChecked value to see what color to give to the button based on whether it says "yes" or "no".
Kindly help! Thanks!
#protocol DetailsTableViewCellDelegate <NSObject>
- (void) customCell:(DetailsTableViewCell *)cell yesBtnPressed:(bool)yes;
#property (nonatomic, retain) NSString * yesChecked;
You'd have to select or deselect them in cellForRowAt. For example if your cell had a leftButton property and you had a model like this, you could do something like the following:
#interface Model : NSObject
#property (nonatomic, assign) BOOL selected;
#end
#protocol CustomCellDelegate <NSObject>
- (void)cellActionTapped:(UITableViewCell *)cell;
#end
#interface CustomCell : UITableViewCell
#property (nonatomic, assign) BOOL leftButtonSelected;
#property (weak, nonatomic, nullable) id<CustomCellDelegate> delegate;
#end
// ModelViewController.h
#interface ModelViewController : UIViewController<CustomCellDelegate>
#end
// ModelViewController.m
#interface ViewController () {
NSArray<Model*>* models;
}
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"reuseIdentifier"];
((CustomCell *)cell).delegate = self;
((CustomCell *)cell).leftButtonSelected = models[indexPath.row].selected;
return cell;
}
- (void)cellActionTapped:(UITableViewCell *)cell {
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// Update data source using (maybe) indexPath.row
}

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.

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