I have one problem regarding UITableview running with Xcode 6.1 + iOS SDK 8.1.
I have one simple single view application, something strange happened when i was using storyboard to create a tableview.
Inside of my viewDidLoad, I wrote this code:
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[NameAndColorCell class] forCellReuseIdentifier:CellIdentifier];
added/edited:
UITableView *tableView = (id)[self.view viewWithTag:1];
NSLog(#"Table View Tag = %ld", (long)tableView.tag);
[tableView registerClass:[NameAndColorCell class] forCellReuseIdentifier:CellIdentifier];
UIEdgeInsets contentInset = tableView.contentInset;
contentInset.top = 20;
[tableView setContentInset:contentInset];
But the tableView is always nil, and eventually caused my App crashed.
I checked with my View Controller's View, both "Use Auto Layout" and "Use Size Classes" were enabled.
After I disabled "Use Size Classes", everything went back normal. I mean, tableView was created successfully.
Has anyone met the same issue? Is this a bug from Xcode? or I made some mistake?
Please help me, Thanks a lot !
Best Regards,
Rui
Related
Cells are blank and xcode displays no error when running my app on ios simulator 9.1 or 9.0. Works fine on 8.4 simulator. There is no change to the code.
I have tried the following
Deleted the custom cell in the storyboard and recreated it. Didn't work
Changed the identifier and file name. Didn't work
Deleted the custom cell .h file from the project and then added it back. Didn't work
After the tableView is visible executed reloadData. Didn't work.
Disconnected then reconnected the tableView delegate and dataSource in the Storyboard. Didn't work.
Used the default customCell.textLabel.text = [_nameArray objectAtIndex:indexPath.section]; and imageView customCell.imageView.image = [_profileImageArray objectAtIndex:indexPath.section]; properties in my custom cell. Worked but this doesn't help because I want to use my custom objects.
Hard coded customName, customTime, and customImage. customCell.customName.text =#"hi"; and customCell.customImage.image = [UIImage imageNamed:#"commentButton.png"];. Didn't work
I added breakpoints to the datasource methods and all breakpoints executed.
I used the NSLog to see if my customCell is nil. I am getting the following response from the log <messagePreviewCell: 0x7fcdf8dd3860; ...>.
I NSLog'ed the nameLabel before setting a value in the cellForRowAtIndexPath and it logs what is set in the storyboard.
I searched my code/storyboard for hidden commands, nothing is set as hidden.
The checkbox for "Use Sizes Classes" is checked and I have set the size to "wCompact hAny".
My code for the cellForRowAtIndexPath found in the viewcontroller.m
static NSString *simpleTableIdentifier = #"messagePreviewCell";
messagePreviewCell *customCell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
customCell.customName.text = [_nameArray objectAtIndex:indexPath.section];
customCell.customTime.text = [_timeArray objectAtIndex:indexPath.section];
customCell.customDescription.text = [_messageArray objectAtIndex:indexPath.section];
NSString *className = [[[_profileImageArray objectAtIndex:indexPath.section] class] description];
if ([className isEqualToString:#"UIImage"]) {
customCell.customImage.image = [_profileImageArray objectAtIndex:indexPath.section];
}
return customCell;
My code was running fine...Please help, thank you!
If your only developing for one device family uncheck "Use Size Classes" in the file inspector of the storyboard.
Found my answer here (additional info)!
UITableView Empty with iOS 9 beta 5 update
EDIT
Built a new sample project from scratch and the dynamic tableview cell height is working flawlessly. I then tried to replicate by trimming down my project to it's bare minimum and it's STILL broken (first few cells buggy). Attaching full project for both working and not working examples. Project/code literally looks 99% identical and for the love of me can't figure out where the 1% difference is. The only thing that is popping out at me is that I used different size classes (wAny hAny vs wCompact hRegular but I can't imagine that would do anything given I'm testing in portrait only
Working project:
Not working project:
WORKING (new project from scratch):
https://drive.google.com/file/d/0B_EIkjmOj3ImWXZjVFZMYXZmVGc/view?usp=sharing
NOT WORKING (my project, cleaned up to it's bare minimum)
https://drive.google.com/file/d/0B_EIkjmOj3ImMGRNOXU2RlNkWEk/view?usp=sharing
Have scoured the web trying to understand what is going on, but for some reason my cell heights are incorrect until I scroll past the prototype cells.
Upon initial load:
And after scrolling past each cell:
Background colors:
cell.postTextLabel.backgroundColor = [UIColor orangeColor];
subView.backgroundColor = [UIColor blueColor];
contentView.backgroundColor = [UIColor brownColor];
Not doing anything too fancy here: just a prototype cell, a subview, and three labels (username, timestamp, text).
Below screenshots highlight my constraints in Storyboard:
I pull data from Parse, and am reloading my data while setting tableView layout
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
[self.tableView reloadData];
And lastly my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self postCellAtIndexPath:indexPath];
}
- (UITableViewCell *)postCellAtIndexPath:(NSIndexPath *)indexPath {
static NSString *PostCellIdentifier = #"PostCell";
PostCell *cell = (PostCell *)[self.tableView dequeueReusableCellWithIdentifier:PostCellIdentifier forIndexPath:indexPath];
[self configurePostCell:cell atIndexPath:indexPath];
[cell setNeedsLayout];
[cell layoutIfNeeded];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}
Finally i found the problem why it is not working. I downloaded the Projects which were uploaded to drive by you, and do modifications in it. And found the issue that you use compact Regular size classes which causes error. After converting your size classes to Any, Any it works fine and perfect.so the key of the solution is change wCompact hRegular to wAny hAny. There is no change in code.
That was initial answer.
Detailed Answer :
1) for the size classes first read this document of apple.
2) In your broken demo the size classes are selected as given below :
This indicates that you selected specific view like compact width and regular height means this auto layout will only work in specific iPhone portrait orientation.
Compact Width | Regular Height combination specifies layout changes
that apply only to sizes resembling iPhone devices in portrait
orientation.
The control wCompact hRegular indicates the compact width and regular
height size classes.
I used below size classes and also in your working project i can be able to see below size classes :
In this Size classes it's working fine.
I don't know is it apple's bug or what. !!!
If you want to support only portrait mode and only iPhones then you can choose iPhone in Development Info -> devices. and for orientation you can choose Portrait and upside down in Device orientation.
Hope this helps you to figure out the problem.
I have met the same issue, just try to use a larger estimatedRowHeight which can contain your content. SDK seems do something wrong when using self-sizing cell.
Just set your row height programatically.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
return rowHieght;
}
for my issue, I set the UILabel font in
layoutSubviews:
thats causing the problem.
after I moved the code to other place, then it works just fine
I also had the same issue,
how-to-add-uiview-in-uitableviewcell
You also have custom cell, so use the following function in customcell class, in your case it is PostCell.m.
- (void)layoutSubviews
{
[super layoutSubviews];
// in my case i only had set frame here while my other
// declarations were in init function, so it solve my problem
}
It solved my same problem, hope it will solve your too, but you have to think it in your own scenario.
First of all you have to set your Detail label line 0 then give leading, trailing and bottom constraints.
in viewDidLoad method add:
tableview.estimatedRowHeight = 50.0 //(your estimated row height)
tableview.rowHeight = UITableViewAutomaticDimension
then return the row height in tableview delegate get method cell row height
In Your code
[cell layoutIfNeeded]
[cell updateContraintsIfNeeded]
are not needed so remove it
Hello: I've been testing my app on iOS 6, 7, and now 8 (beta 5). My UITableView with custom UITableViewCells is working fine on 6 and 7. However, on iOS 8, I'm getting a crash when I attempt to access a subview (text field) of a cell.
I am aware of the fact that there's another view in the cell's hierarchy in iOS 7. Strangely, it appears that this isn't the case in iOS 8. Here's the code I'm using:
//Get the cell
CustomCell *cell = nil;
//NOTE: GradingTableViewCell > UITableViewCellScrollView (iOS 7+ ONLY) > UITableViewCellContentView > UIButton (sender)
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
cell = (CustomCell *)sender.superview.superview;
} else {
cell = (CustomCell *)sender.superview.superview.superview;
}
//Get the cell's index path
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//etc.
NSLog(#"%#", cell.textField); //<---Crashes here
So, as you can see, I'm accounting for the extra view in iOS 7. After adding some breakpoints and taking a closer look at the variables, I see that cell exists, but all the subviews it has in the interface file (which are linked up) - including textField - are nil. At the line specified, I'm receiving the following crash log:
-[UITableViewWrapperView textField]: unrecognized selector sent to instance 0x12c651430
I looked into this further, and I found this:
Changing the else statement to be identical to the preceding line gets rid of the crash, and the app works fine (using sender.superview.superview like in iOS 6).
This makes no sense to me. Did Apple revert the hierarchy of UITableViewCells to that of iOS 6's, or am I missing something? Thanks!
I've encountered the same issue. Here's a more reliable method:
UITextField* textField = (UITextField*)sender;
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:[self.tableView convertPoint:textField.center fromView:textField.superview]];
This will work regardless of the underlying view hierarchy of UITableViewCell.
Different iOs versions have different implementations of UITableViewor UITableViewController, as an easy fix you'll have to iterate through superviews until you found the desired view class instead of relying it being the N-th superview.
I also had the same issue on iOS8 with getting UITableViewCell via superview from a child view. This is what I came up with for both iOS7 and iOS8 support.
-(void)thumbTapped:(UITapGestureRecognizer*)recognizer {
NSLog(#"Image inside a tableview cell is tapped.");
UIImageView *mediaThumb = (UIImageView*)recognizer.view;
UITableViewCell* cell;
// get the index of container cell's row
// bug with ios7 vs ios8 with superview level!! :(
/*** For your situation the level of superview will depend on the design structure ***/
// The rule of thumb is for ios7 it need an extra superview
if (SYSTEM_VERSION_LESS_THAN(#"8.0")) { // iOS 7
cell = (UITableViewCell*)mediaThumb.superview.superview.superview.superview;
} else { // iOS 8
cell = (UITableViewCell*)mediaThumb.superview.superview.superview;
}
}
Just to clarify, in cellForRowAtIndexPath I assigned the tap gesture recognizer. The original design is very complex in Storyboard with many subviews, buttons.. etc.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MediaCell" forIndexPath:indexPath];
.......
UIImageView *mediaImage = ............
.....................................
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(thumbTapped:)];
[mediaImage addGestureRecognizer:tapGestureRecognizer];
return cell;
}
I upgraded to Xcode 5 and my app to target iOS 7 and my app no longer responded to scrollToIndexPath to scroll the appropiate tableviewcell out of the way of the keyboard
My set up is i had a custom tableview cell, with textfield on it, and was using it to generate multiple tableview cells to display and edit parts of an address.
Tapping on a textfield on a cell triggers
- (void)textFieldDidBeginEditing:(UITextField *)textField
And the code to make it scroll to the top , out of the way of the keyboard was as follows
UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
[self scrollToRowAtIndexPath:[self indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
This worked fine in iOS 6 but doesn't work in iOS 7. It also does not generate any error.
The solution here is a simple one
It appears that there has been a change in the hierarchy of tableviewCells
So adding an extra .superview
UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
Becomes
UITableViewCell *cell = (UITableViewCell*) textField.superview.superview.superview;
and it works targeting iOS 7.
The textField.superview.superview used to be an object of class UITableViewCell (or more specifically my custom UITableViewCell)
Now it's an object of class UITableViewCellScrollView and you need to get that its superview.
UITableViewCellScrollView is a private subclass that apparently enables the the slide left to reveal the delete button
After working this out i found this blogpost which illustrates it nicely
Hope this stops a few of you pulling your hair out :)
Simon
As the previous answer said, the view hierarchy of a UITableViewCell changed in iOS 7. Instead of using:
UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
Define the following method:
- (UITableViewCell *)cellForSubview:(UIView *)subview
{
UIView *view = subview;
while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
view = [view superview];
}
return view;
}
Then use it as follows:
UITableViewCell *cell = [self cellForSubview:textField];
This should work in iOS 6 and 7.
When using the iOS 6 SDK and running my app in the iPhone 5.0 simulator my tableview appears just fine as a grouped style tableview. However when I run my in the iPhone 6.0 simulator it appears as a UITableViewStylePlain tableview.
Any ideas what would cause this strange behavior? I'm not doing anything too crazy in the tableview besides a textview inside a tableview cell.
I have a grouped tableview that is working correctly in iOS6 using the code:
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height) style:UITableViewStyleGrouped];
[tableView setDataSource:self];
[tableView setDelegate:self];
Are you using the Interface builder to create this grouped tableview or are you creating it programmatically? There seems to be quite a few issues with iOS6 and previously created interfacebuilder views.
If you are using the IB to create things can you try to re-create the tableview in code (redundant and useless, I know, but it may show what is the problem).
Somewhere in your viewDidLoad() function put
if(tableView)
{
[tableView removeFromSuperview];
}
tableView = [[UITableView alloc] initWithFrame:tableView.frame style:UITableViewStyleGrouped];
[tableView setDataSource:self];
[tableView setDelegate:self];
[self addSubview:tableView];
This may be poor coding or cause memory leaks if not using arc, but it would be interesting to see if you get a grouped styling that way.
This code solved the ios6 grouped tableview problem for me:
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;