Xcode 9 - iOS 11 UITableView rows are empty - objective-c

Ever since the new iOS 11 update, I have an app that will show blank tableview rows on the simulator and device. Even the row separators will not show for any of the rows that are supposed to be there. If I change the simulator to an older iOS version, the rows will show fine. No changes to code or storyboard.
The rows still have the data, meaning I can tap on one of the blank rows and it will execute the code and contain the information I was expecting.
It appears that other scenes that I have where the tableview is placed on a view and I don't use the built in text label work fine. Just this tableview class using the built in text label.
Here is my code for the tableview class...
#interface BunkPickTableViewController ()
#end
#implementation BunkPickTableViewController
#synthesize appDelegate, delegate, bunkPassedValue;
- (void)viewDidLoad {
[super viewDidLoad];
UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
CAGradientLayer *bgLayer = [BackgroundLayer tanGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
self.tableView.backgroundView = backgroundView;
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
self.title = #"Bunk Codes";
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:NO animated:NO];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.bunkArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
}
Bunk *bunkObj = [appDelegate.bunkArray objectAtIndex:indexPath.row];
cell.textLabel.text = bunkObj.bunkId;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
Bunk *bunkObj = [appDelegate.bunkArray objectAtIndex:indexPath.row];
[delegate dismissBunkPop:bunkObj.bunkId];
[self.navigationController popViewControllerAnimated:YES];
}
#end
TableView Settings Image

I had this issue as well with IOS 11 showing blank cells.. but in IOS 10 was fine. My issue was that I was using a gradient which for whatever reason stopped the cell text from being shown. Removing the gradient resolved the blank cells.

This is probably because your tableView gets under bgLayer.
The problem seems to be in self.view.layer insertSublayer:bgLayer atIndex:0. I was using insertSubview at index 0 and I had the same problem. This is probably a bug in iOS 11 - if your tableView is defined in storyboard, it always gets pushed to back.
The best solution is to put the bgLayer inside the tableView.backgroundView.
NOTE:
You could also solve it by calling sendSubviewToBack on the bgLayer in viewDidAppear, but unfortunetaly, tableview cells are moving to back on every tableview reloaddata, so it is not a good solution.

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.tableView.bounds;
gradient.colors = #[(id)[UIColor blackColor].CGColor, (id)[UIColor
grayColor].CGColor, (id)[UIColor
lightGrayColor].CGColor];
UIView *bgView = [[UIView alloc]
initWithFrame:self.tableView.bounds];
[self setBackgroundView:bgView];
[bgView.layer insertSublayer:gradient atIndex:0];
[self.tableView setBackgroundView:bgView];
Setting gradient layer for tableview's background view solved the problem for me. Upto iOS 10 we can directly insert sublayer to tableView but in iOS 11 layer should be inserted to UITableVIew's background view only.

In tandem with the aforementioned tableview subviews manipulation, this problem can also occur if your project existed pre Xcode 9 and you then checked Use Safe Area Layout Guides on your storyboard. Try unchecking that and see if it works.

Related

Insert row in UiTableView does not update rest cell’s indexPath?

Say i’m loading UITableview with each UITextView inside each cell as subview.And i’ve assigned indexPath.row as tags for each textview.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"userDetails";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 60)];
NSString * myString = [contentArray1 objectAtIndex:indexPath.row];
textView.text= myString;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
[tapRecognizer setNumberOfTouchesRequired:1];
[tapRecognizer setDelegate:self];
textView.userInteractionEnabled = YES;
textView.tag = indexPath.row;//assign tags to textview
[textView addGestureRecognizer:tapRecognizer];
[cell addSubview:textView];
return cell;
}
Below method gets called once the user taps on any textview.I’m seeing proper tag values printed when i tap on any textviews.
-(void) action:(id)sender
{
//NSLog(#"TESTING TAP");
UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer *)sender;
NSLog (#"%d",[tapRecognizer.view tag]);
}
Now i would like to insert row in my tableview,say at index 3.
What i did is simple,
[contentArray1 insertObject:[NSString stringWithFormat:#"added cell”] atIndex:3];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
Now when i try to tap on any textviews after the inserted cell,i’m able to see the old tag values.Meaning,after row gets inserted to tableview at index=3,when i tap on textview i can see tag=2,then again when i tap on next cell’s textview i can see tag=2,it should be 3.
My question is,once we insert any row/cell in tableview,the tableview will not refresh other cell tags/index?….
I can fix it by calling reloadVisibleCells method.But i’m looking out for better solution.I don’t want to refresh whole screen just for inserting a row.Any solutions would be greatly appreciated.
Try to do it like this:
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
Your problem is that inserting/deleting rows doesn't cause reloading of other rows, and this is the expected and correct behavior. However, since -tableView:cellForRowAtIndexPath: is not called for those other rows, they are still configured with old (now obsolete) tags.
You can fix it in a number of ways (off the top of my head):
subclass UITableViewCell and store the represented object itself as its property (instead of the object's index)
associate the object with the UITableViewCell with objc_setAssociatedObject()
use -[UITableView indexPathForCell:] instead of tags to figure out a cell's real index path.

not able to click the search bar tableview cell

I've got a tableview containing array of names. The search bar works perfectly filtering the names in the table view.
The problem is the didSelectRowAtIndexpath is not getting fired when clicking the search tableview cell. Could you help me out?
What is the thing that I'm missing? Should I include any special delegate to involve search tableview cell click.
Below is the image and code.
-(void)search
{
nameArray = [[NSMutableArray alloc] init];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
self.tableViewFriendsList.tableHeaderView = searchBar;
}
- (void)searchDisplayController:(UISearchDisplayController *)controller
willShowSearchResultsTableView:(UITableView *)tableView
{
[tableView setRowHeight:70];
[tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.tableViewFriendsList) {
NSString *friendsID =[[[self.friendsDictionary objectForKey:#"data"] objectAtIndex:indexPath.row] objectForKey:#"id"];
[[FacebookHelper sharedFacebookHelper] postOnWallWithDelegate:self andID:friendsID];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(#"I ve come here");
NSString *friendsID =[friendsListIdArray objectAtIndex:indexPath.row];
[[FacebookHelper sharedFacebookHelper] postOnWallWithDelegate:self andID:friendsID];
}
}
You forgot to set
searchController.searchResultsDelegate = self;
I do something in one of my projects that may be of assistance:
// add gesture to detect when table view is being tapped so that keyboard may be dismissed
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissKeyboard)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:gestureRecognizer];
Moreover I am wondering why you have a search bar within a table cell. Would you mind posting a screen shot of it in your app? I am afraid you may be doing more work than is necessary.

Tablecell click short delay

When I click on table cell, there's a short delay of 1-2 second before it loads the next view. I've seen some apps that show an activity indicator during that time and that's what I'd like to do. I've added one like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinner.frame = CGRectMake(200,200,200,200);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
VenueViewController *vviewcontroller = [[VenueViewController alloc] initWithNibName:#"VenueViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:vviewcontroller animated:YES];
[vviewcontroller release];
vviewcontroller = nil;}
however this also appears with a delay, and just before the next view is showing. It seems the app freezes for 1-2 seconds after clicking on the table cell so it doesn't even show the activity indicator.
I think the secret is that you should call load method using a performSelector method. Another tip is hiding or showing the activity so it won't consume time this operation.
So this could be a pseudocode of that
Inside your ViewController class definition:
IBOutlet UIActivityIndicatorView *spin; // created in view and hidden
In your implementation...
-(void) load{ // your code
VenueViewController *vviewcontroller = [[VenueViewController alloc] initWithNibName:#"VenueViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:vviewcontroller animated:YES];
[vviewcontroller release];
vviewcontroller = nil;
spin.hidden=YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
spinner.hidden=NO;
[self performSelector:#selector(load) withObject:nil afterDelay:0];
}
Hope it helps.

UITextField unable to become first responder if added to UITableViewCell via accessoryView

Like many other users, I'm trying to set throw a UITextField into a UITableViewCell to be used for editing a row. But instead of adding a new view, I'm trying to use the accessoryView property. At first I tried this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = textField;
[textField becomeFirstResponder];
}
...and the UITextField is added properly, but I need to tap it again to get the keyboard to show. However if I do this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell addSubview:textField];
[textField becomeFirstResponder];
}
...it works, the keyboard shows up but, I don't get any of the other benefits of having it as the accessoryView (easier positioning with other views moving around it). I suppose the addSubview: call is altering the responder chain or something and allowing my UITextField to become the first responder, but thought maybe there was another way to do this allowing me to set the cell's accessoryView instead. Thanks.
I hate my solution, but it works and am up for a better way.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect detailTextLabelFrame = cell.detailTextLabel.frame;
detailTextLabelFrame.origin.x -= 100.0f;
detailTextLabelFrame.size.width += 100.0f;
UITextField *textField = [[UITextField alloc] initWithFrame:detailTextLabelFrame];
cell.detailTextLabel.hidden = YES;
cell.accessoryView = textField;
// These are the two lines that made it work, but it feels ugly
[cell.accessoryView removeFromSuperview];
[cell addSubview:cell.accessoryView];
[cell.accessoryView becomeFirstResponder];
}
I hope you know about textfield delegate.Try this way....may be it will help you.
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if(theTextField == yourtextfieldname) {
[yourtextfieldname resignFirstResponder];
}
return YES;
}

cellForRowAtIndexPath not being called

I have a view that adds another view on top in this manner:
- (void)showAreaEditView {
NSLog(#"SHOWING AREA EDITOR VIEW");
if (self.thisAreaEditorView == nil) {
// Create View
AreaEditorView *tmpViewController = [[AreaEditorView alloc] initWithNibName:#"AreaEditorView" bundle:nil];
self.thisAreaEditorView = tmpViewController;
[tmpViewController release];
// Hide the back button
self.thisAreaEditorView.navigationItem.hidesBackButton = YES;
}
self.thisAreaEditorView.myInspectionID = self.myInspectionID;
self.thisAreaEditorView.loggedIn = loggedIn;
self.thisAreaEditorView.loggedInGroup = loggedInGroup;
// Slide view up
[self.view addSubview:thisAreaEditorView.view];
CGRect endFrame = CGRectMake(self.view.frame.size.width/2 - thisAreaEditorView.view.frame.size.width/2,
self.view.frame.size.height/2 - thisAreaEditorView.view.frame.size.height/2,
thisAreaEditorView.view.frame.size.width,
thisAreaEditorView.view.frame.size.height);
CGRect startFrame = endFrame; // offscreen source
// new view starts off bottom of screen
startFrame.origin.y += self.view.frame.size.height;
self.thisAreaEditorView.view.frame = startFrame;
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.6];
thisAreaEditorView.view.frame = endFrame; // slide in
[UIView commitAnimations];
}
I'm sure you can just ignore the slide part, I feel the addSubview is relevant.
Then in thisAreaEditor I have the view with the table and buttons and such. UITableView delegate/datasource is going to File's Owner as normal.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"numberOfRowsInSection returning %d", [tableData count]);
[tableData count];
}
This function numberOfRowsInSection returns 4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSString *thisText = [tableData objectAtIndex:indexPath.row];
cell.textLabel.text = thisText;
NSLog(#"looking at cell %d text:%#", indexPath.row, thisText);
return cell;
}
But cellForRowAtIndexPath never gets called.
I'm at a loss here, I have no idea how it can seem to work fine but one of the delegate functions simply not be called.
I have tried [bigTable reloadData] and so on, the table just never gets populated and no logs from the function output.
Thanks in advance.
You might have just edited this out, if so I'm sorry, but it looks like you forgot to return tableData's count.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(#"numberOfRowsInSection returning %d", [tableData count]);
    return [tableData count];
}
It seems you're missing UITableViewDelegate.
If you're using Interface Builder, right click the table view outlet and drag both delegate and datasource to File's Owner.
And if not using Interface Builder add this where you init your tableView
bigTable.delegate = self;
bigTable.dataSource = self;
Remember to import the UITableViewDelegate and UITableViewDataSource protocols, just as Srikar says.
Hope this is to any help.
Cheers!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is also not called when the tableview's height is not set.
Maybe you did not set the tableView delegate as self or the datasource as self. Add this code & see if it works now -
[tableView setDelegate:self];
[tableView setDataSource:self];
Also in your header file inherit these delegates - UITableViewDelegate, UITableViewDataSource
#interface yourViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
Hope this helps.
This is a older link, but I wanted to update this with putting info on how I resolved this issue.
For me the issue was the Array to populate the table had 0 rows so cellForRowAtIndexPath was never called.
Make sure that the Array you are using to populate the table has data in it.