Data is disappearing from UITableView - ios

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"];

Related

Change image on click in custom table cell made from nib

I have a table with custom cells that are made from a nib file. When a user clicks a cell it expands in height , thus simulating a dropdown. In the custom cell there is an imageView that has a dropDown image when the cell is not clicked. However when the cell is clicked it should change the image to a collapse image or arrow up image to show that the cell is open.
I am having a problem changing the image when the cell expands from arrow down to arrow up and vice versa. I would like assistance achieving this.
Here is my code :
ViewController.h
#interface ViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property(nonatomic, strong) NSArray *items;
#property (nonatomic, assign) BOOL expandFlag;
#property (nonatomic, assign) NSIndexPath* selectedIndex;
ViewController.m
- (NSArray *) items
{
if (!_items) {
_items = [NSArray new];
}
return _items;
}
- (void)viewDidLoad {
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:#"CustomTableViewCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:#"cell"];
_items = #[
#{#"title":#"Simpson", #"short":#"Homer", #"long":#"Mr jhvjm,b;k t tyfy rctrc rtcf rvty rthvgh bb r5ertvyg hg r5 tyhg 6ruygj 8rutyj r6yiugg tygdtjhgfdt hg tvt gthgfgni7yjftgjhb ftgfh b yjh gtfjfyhh j jj j", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Simpson", #"short":#"Marge", #"long":#"Mrs bjyvhm uikn o utv jb k", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Simpson", #"short":#"Bart", #"long":#"Mr vubj cbjknuy iubyuvjh biubkj ", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Simpson", #"short":#"Lisa", #"long":#"Miss jbjvjbbiuvu yuvhj uby ", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Simpson", #"short":#"Maggie", #"long":#"Miss iubniyujh k iuuh ", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Flanders", #"short":#"Ned", #"long":#"Mr hbuyvj iybkj nui uhc n", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."}
];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) didExpandCell{
_expandFlag = !_expandFlag;
[self.tableView reloadRowsAtIndexPaths:#[_selectedIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.items.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
NSDictionary *item = _items[indexPath.row];
cell.titleImage.text = [item objectForKey:#"title"];
cell.longLabel.text = [item objectForKey:#"long"];
cell.shortLabel.text = [item objectForKey:#"image"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_selectedIndex = indexPath;
[self didExpandCell];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_expandFlag && _selectedIndex == indexPath) {
return 400;
}
return 200;
}
CustomeTableViewcell.h
#interface CustomTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *thumbImage;
#property (weak, nonatomic) IBOutlet UILabel *titleImage;
#property (weak, nonatomic) IBOutlet UILabel *shortLabel;
#property (weak, nonatomic) IBOutlet UILabel *longLabel;
#property (weak, nonatomic) IBOutlet UIImageView *dropDownImage;
CustomerTableViewCell.m
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
You should check and change dropDownImage in cellForRowAtIndexPath method. Something likes the code below
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
NSDictionary *item = _items[indexPath.row];
cell.titleImage.text = [item objectForKey:#"title"];
cell.longLabel.text = [item objectForKey:#"long"];
cell.shortLabel.text = [item objectForKey:#"image"];
NSString *dropDownImageName = [indexPath isEqual:_selectedIndex] && _expandFlag ? #"ARROW_DOWN" : #"ARROW_UP";
cell.dropDownImage.image = [UIImage imageNamed:dropDownImageName];
return cell;
}
Change ARROW_UP and ARROW_DOWN with your image names.

Table view scrolling all the way down not working

I got 3 labels on my view displaying some stuff, a table view below (beginning at screen halfway down) and
a data retrieval method where I fill up an NSArray for the table view.
Everything works, the problem now is if there are more than a few elements in the array for the table view (more than the screen can display at once) I'm not able to scroll down to the bottom, it's always autoscrolling up again, as if the table view was empty space there (which it is not, I checked of course).
It worked on the previous screen the way I did it, I think the iOS doesn't notice the labels or something similar - it thinks the table view has the whole screen for itself. I tried resizing the table view in the xib to let it end just before the screen ends but the table view occupies all screen beginning from where it started. I also tried to
initWithFrame:CGRectMake...
But this did not work as well. Any ideas whats going on here? I'd be really glad about some help, thanks guys!
EDIT:
.h file
#interface ZProjekt : UIViewController <RetailHeaderTabSource, MFMailComposeViewControllerDelegate , UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate, UITableViewDataSource>
{
NSString *messageBody;
NSString *subjectBody;
NSMutableArray *fieldsInTable;
int fieldscounter;
}
#property (strong, nonatomic) IBOutlet UILabel *tableNamed;
#property (strong, nonatomic) IBOutlet UILabel *objectsInTable;
#property (strong, nonatomic) NSArray *DetailModal;
#property (strong, nonatomic) IBOutlet UILabel *mboSyncGroup;
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#end
here is the code of the tableview .m file
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return fieldscounter;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = fieldsInTable[indexPath.row];
[Constants updateLabelValues:cell.textLabel withKey:k_IPhone_DetailCell_HeaderLabel];
return cell;
}
Try this one :
[tableView setContentOffset:CGPointMake(tableView.frame.size.width, tableView.frame.size.height) animated:NO];
or
on the TableViewController->Table View check the clip Subviews n Autoresize Subviews.
Also,
make sure the total rows are the same with the total contents in the tableView which is 3
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
Problem Found ! The problem is, the tableview doesn't know which table you are scrolling either the last one or the previous one. You need to re-assign the value to the NSMutableArray. You need to store the content of the array in the table1 into array or dictionary and you need to reassign in the CellForRowIndexPath. I had the same problem like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView != table2.tblView) {
fieldsInTable = table1.fieldsInTable;
}
else
{
fieldsInTable = table2.fieldsInTable;
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = fieldsInTable[indexPath.row];
[Constants updateLabelValues:cell.textLabel withKey:k_IPhone_DetailCell_HeaderLabel];
return cell;
}

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.

Custom UITableViewCell Created in StoryBoard Results in duplicated UILabels

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.

Resources