All UITableCells disappear when tapping on UITableView in iOS 7 - ios

I am having a problem with my UITableView in iOS7. Initially, the data loads in just fine, and I get output in my console that proves that the cells are speaking to the data source correctly, but as soon as I tap anywhere on the table, the cells disappear and the table goes blank. The height of the cells in the empty UITableView seem to be honoring the height my custom prototype cell (410px), but all the data in the cells vanish, and the empty table view acts like it only has one cell in it (like its default state before it gets hooked up to the delegate).
I am using Storyboards for this app.
To get a little context, this app is similar to the iphone Instagram app, and I am using this application as way to learn iOS 7 development. I have been banging my head up against a wall trying to solve this issue, and I can't find any online resources that can help me solve this, so I wanted to ask all the smart peeps on Stack Overflow.
I have prepared a graphic that helps you see the problem
higher resolution version here
Here is my TableViewController code:
#interface PA_PhotoTableViewController ()
#property (nonatomic, copy) NSArray *photos;
#end
#implementation PA_PhotoTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.photos = [[PA_PhotoStore sharedPhotoStore] allPhotos];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[PA_PhotoStore sharedPhotoStore] allPhotos] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PA_PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PhotoCell" forIndexPath:indexPath];
PA_Photo *photo = (self.photos)[indexPath.row];
cell.photoTitle.text = photo.title;
cell.photoOwnerName.text = [NSString stringWithFormat:#"%#", photo.owner];
cell.photoLikes.text = #"99";
// Photo Image URL
NSURL *photoImageURL = [NSURL URLWithString:photo.image_full_url];
[cell.photoImage sd_setImageWithURL:photoImageURL placeholderImage:[UIImage imageNamed:#"lightGraySpinningLoader.gif"]];
// Photo Owner Image
[cell.photoOwnerImage sd_setImageWithURL:photoImageURL placeholderImage:[UIImage imageNamed:#"lightGraySpinningLoader.gif"]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// This code never gets called when I try to tap on a cell
NSLog(#"A row was selected");
}
- (void)dealloc {
NSLog(#"dealloc called in PA_PhotoTableViewController");
}
and here is the custom cell code PA_PhotoCell (consolidated .h & .m files):
#interface PA_PhotoCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UIImageView *photoImage;
#property (nonatomic, weak) IBOutlet UILabel *photoTitle;
#property (nonatomic, weak) IBOutlet UILabel *photoOwnerName;
#property (nonatomic, weak) IBOutlet UIImageView *photoOwnerImage;
#property (nonatomic, weak) IBOutlet UILabel *photoLikes;
#property (nonatomic, weak) IBOutlet UILabel *photoTimestamp;
#property (nonatomic, weak) IBOutlet UILabel *photoComments;
#end
#implementation PA_PhotoCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
NSLog(#"in set selected");
}
-(void)setHighlighted:(BOOL)highlighted {
NSLog(#"in set highlighted");
}
You can see a few NSLog() calls to help me see if anything is getting called.
Where am I going wrong? The end goal is to click on one of the TableViewCell instances and launch a UINavigationController, I know how to do that, but I can't move on to that step until I figure out why my UITableView won't scroll, and why it disappears when I click on it!
EDIT: After much testing, debugging and experimentation, I have been able to conclude that the problem is actually not with the UITableView at all, and it is, in fact, a problem with how the UITableView is being loaded into its parent view. I still haven't found a solution to my problem, but I am getting closer to finding the cause. Here is what I have discovered:
First, when any of the UIButtons at the bottom of the screen are tapped (see photo reference), it loads the relevant instance of UIViewController into a UIView called placeholderView. When I run my problematic UITableView OUTSIDE of this UIView (where the UITableViewController is acting on its own, not embedded within another UIView) then the table works perfectly, it scrolls, it revives click events, and so on. So as soon as I load the UITableView into the UIView, the UITableView becomes unresponsive (it doesn't scroll or receive tap events) and any attempt to interact with it, the UITableView goes completely blank. My debugging session concludes that the NSArray *photos never gets reset to nil, or manipulated in anyway, the table just goes blank.
So does anyone have any ideas on what would cause a UITableView to do this when being loaded into a generic UIView? All the other views that get loaded into this generic UIView are responsive, and behave as expected. Its just this UITableView that is giving me problems.
If you review the graphic I attached to this post (above), you will see that I am using what appears to be a UITabBarView, but it is, in fact, just a generic view with UIButtons inside. The reason I decided to craft my own "UITabBarView look-alike" instead of using the ready-made UITAbBarView class was because I wanted to give custom functionality to the "menu" button on the bottom left (I want a nice UIView to slide in from the left, and stop about 60 pixels from the right of the screen when the "menu" button is tapped, and I can't figure out how to customize the behavior of the UITabBarView, so I opted for this approach.
Here is the code that is actually loading the UITableViewController into the subview (via a CustomStoryboardSegway):
// PA_HomeViewCustomStoryboardSegue.m
#import "PA_HomeViewCustomStoryboardSegue.h"
#import "PA_HomeViewController.h"
#implementation PA_HomeViewCustomStoryboardSegue
// to create a custom segue, you have to override the perform method
-(void)perform {
// get the source and destination view controllers
PA_HomeViewController *segueSourceController = (PA_HomeViewController *)[self sourceViewController];
UIViewController *destinationController = (UIViewController *)[self destinationViewController];
for (UIView *view in segueSourceController.placeholderView.subviews){
[view removeFromSuperview];
}
segueSourceController.currentViewController = destinationController;
[segueSourceController.placeholderView addSubview:destinationController.view];
}
#end
and here is the header file for my PA_HomeViewController (the view the contains the "placeholderView" which is the target view that loads the various UIViewControllers after the user has tapped the UIButtons at the bottom of the view (similar to a TabBarView) :
#interface PA_HomeViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *placeholderView;
#property (weak, nonatomic) UIViewController *currentViewController;
#end
I am hoping that I am just missing something obvious in the way that I am loading the UITableView into the placeholderView, and something in there is causing the UITableView to go completely blank.

When you display the UITableView in a different view, you must always make sure that the view controller which "hosts" the UITableView has a strong reference to its controller. In your case, the data source for the UITableView seems to be deallocated after adding the UITableView as subview.
Changing the currentViewController property from weak to strong should fix your problem.

In swift you need to declare viewcontroller object globally that would result in Strong, in case if you declare locally it results in keep disappearing the cells.
e.g.
var refineViewController : RefineViewController?
then you can access that controller using below code that would not result in disappearing cells.
func showRefineView(isFindHomeTab isFindHomeTab : Bool){
refineViewController = RefineViewController(nibName: String(BaseGroupedTableVC),bundle : nil)
refineViewController!.refineSearchDelegate = self
refineViewController!.view.frame = CGRectMake(0, -490, self.view.frame.size.width, self.view.frame.size.height)
UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseOut, animations:
{
self.refineViewController!.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
self.refineViewController!.isFindHomeTab = isFindHomeTab
}, completion: nil)
self.view.addSubview(refineViewController!.view)
}

I experienced the exact same problem. The issue was that I was using a custom datasource class (called tableVCDataSource), and was setting the tableView's dataSource incorrectly in the ViewController class. I was doing:
override func viewDidLoad() {
mainTableView.dataSource = TableVCDataSource()
}
when I should have been doing:
fileprivate var tableVCDataSource: TableVCDataSource?
override func viewDidLoad() {
tableVCDataSource = TableVCDataSource()
mainTableView.dataSource = tableVCDataSource
}
This solved my issue.

Related

UICollectionView : Cells do not appear/populate

I need some help with a uicollectionview that I've been trying to implement in Objective-C/xcode: I am unable to have the cells appear/populate the collectionview (CV).
Currently I am using the interface builder, and a custom class for cells.
The CV is to be a wide strip that is a single row of square image cells, that can be scrolled/swept through horizontally.
My attempts at implementation result in the wide strip but it never populates with cells.
I have explored other stackoverflow threads similar to this issue, where they discuss the subclassing of cell but I was unable to make it work. Have also tried programmatic implementation and had the same issue. Would really appreciate any insight as I have been stuck at this point for days. Below the code I am using, are screenshots of interface builder showing different settings... some I know are useful to include and I apologize if I have shared anything useless.
from ViewController.h
#interface ViewController : UIViewController <UICollectionViewDelegate,UICollectionViewDataSource>
#property (weak, nonatomic) NSMutableArray *medCollectionCells;
from ViewController.m
#import "CoolCell.h"
#property (weak, nonatomic) IBOutlet UICollectionView *medCollection;
- (void)viewDidLoad
{
_medCollectionCells = [NSMutableArray arrayWithObjects:#"1st cell",#"2nd cell",#"3rd cell",#"4th cell",#"5th cell",#"6th cell",#"7th cell",#"8th cell",#"5th cell", nil];
self.medCollection.delegate = self;
self.medCollection.dataSource = self;
}
// collection view delegate methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [_medCollectionCells count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CoolCell *cell = (CoolCell *)[_medCollection dequeueReusableCellWithReuseIdentifier:#"MyCustomCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
return cell;
}
CoolCell.h:
#import <UIKit/UIKit.h>
#interface CoolCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UILabel *customLabel;
#property (weak, nonatomic) IBOutlet UIImageView *customImage;
#end
CoolCell.m:
#import "CoolCell.h"
#implementation CoolCell
#end
Screen Shots
Storyboard layout
Collection View Settings 1
Collection View Settings 2
Cell Settings 1
Cell Settings 2
Interface Builder Outlets
Grateful for any assistance/insight. Thank you.
Seems like the delegate and dataSource properties are connected to a View object in Interface Builder. Maybe remove that since you are setting those properties in code.

Delegates and Protocols for UIButton toggle in objective c [duplicate]

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
}

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
}

UITableView not displaying when embedded in another view

I am trying to create a view with three big subviews side-by-side. The three subviews should all be UITableViews packaged in UINAvigationControllers. I am using a subclass for UITableViewController named "TOGViewController" here.
When I do set this up, the UITableViews are not updating. The header (from the UINAvigationController) appears, but the space below stays white. When I debug it, I can see that cellForRowAtIndexPath is NEVER called. numberOfSectionsInTableView, numberOfRowsInSection and even heightForRowAtIndexPath are all called. But cellForRowAtIndexPath never!
CODE (only for the first, leftmost subview)
// Create a big scrollview 3 times as wide as the screen and paginate it
CGRect screenRect = self.view.bounds;
CGRect wholeViewRect =screenRect;
wholeViewRect.size.height= screenRect.size.height;
wholeViewRect.size.width= screenRect.size.width * 3.0;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: wholeViewRect];
scrollView.contentSize = wholeViewRect.size;
scrollView.contentOffset = CGPointMake(screenRect.size.width, 0);
scrollView.pagingEnabled = TRUE;
self.view = scrollView;
// Add a view to the left
CGRect firstRect = screenRect;
firstRect.size.width -= 1;
TOGViewController *insightsViewController = [[TOGViewController alloc] init];
insightsViewController.view.frame = firstRect;
[self addChildViewController:insightsViewController];
[scrollView addSubview: insightsViewController.view];
When I am using exactly the same TOGViewController (subclass of UITableViewController) and place this directly on the screen it works fine. But as soon as I embed this into a scrollview with two other UITableViews, none of the table views displays anything.
EDIT: I pasted the wrong code in the question from an older version. There are no UINavigationController around each UITableViewController. There is only one UINavigationController for the whole triple view consisting of 3 table views.
If you want multiple UITableViews in one view, each with their own Navigation Controller, you'll want to put each TableView in its own Container View.
Check Apple's documentation and various tutorials available online.
There are various other questions that deal with this topic. The key is to make each UITableView a property of your View:
#property (nonatomic, strong) IBOutlet UITableView* firstTableView;
#property (nonatomic, strong) IBOutlet UITableView* secondTableView;
#property (nonatomic, strong) IBOutlet UITableView* thirdTableView;
You would only keep one delegate and datasource for all of them. Then in your UITableView delegate and datasource methods you would include if statements to check which UITableView you are in.
For example:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (tableView == self.firstTableView) {
...
} else if (tableView == self.secondTableView) {
...
} else {
//thirdTableView code
}
}
It sounds like something is messed up with UITableViewDataSource Protocol delegate property on your TableViews. tableView:cellForRowAtIndexPath: is called on the tableView.datasource object. Although I am kinda confused as how tableView:numberOfRowsInSection: is getting called but NOT tableView:cellForRowAtIndexPath:.

Stuck with tableview

I am making an app with dynamic tables inside of it. I had managed to let it work with the table inside the MainviewController, but now i am stuck. I tried to put the table code in its own class.
strange thing is that it gives no errors.
This is what i do:
MainviewController.m:
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * 0;//0 will later be i
frame.origin.y = 0;
frame.size.width = self.scrollView.frame.size.width;
TellerTable *tellerTable = [[TellerTable alloc] initWithFrame:frame style:UITableViewStyleGrouped];
[tellerTable loadWithJsonData:JSONArray];
[tellerTable setDataSource:tellerTable];
[self.view addSubview:tellerTable];
[tellerTable reloadData];
TableTeller.m:
-(void)loadWithJsonData:(NSArray *)JA
{
self.JSONArray = JA;
tableview = self;
self.delegate = self;
self.dataSource = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *TellerItems = [JSONArray objectAtIndex:section];
return TellerItems.count;
}
(the rest to make the table cellForRowAtIndexPath etc..)
TellerTable.h
#interface TellerTable : UITableView <UITableViewDataSource, UITableViewDelegate>
{
UITableView *tableview;
}
#property (nonatomic, retain) IBOutlet UITableView *tableview;
#property (nonatomic, retain) IBOutlet NSArray *JSONArray;
-(void)loadWithJsonData:(NSArray *)JA;
#end
Strange thing is, it goes to loadwithJSON, then it calls numberOfRowsInSection but after that it stops. It doesn't add the table to the scrollview. What am i doeing wrong/forget?
I think you should change your approach .. you have created a subclass of UITableView and then another tableView property on the same then you are assigning it to self all feels wrong even if it works. I dont see any use of subclassing here you are not customising the table so why are you subclassing it?. Add a tableView(drag and drop) to xib or storyboard then connect outlet to mainviewcontroller. You may be able to make it work somehow using this method but its errorprone,less readable etc etc

Resources