iOS: implementation of custom UITableViewCell needs to call super - ios

"* Assertion failure in -[PlayingCell layoutSublayersOfLayer:],
/SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776 2013-11-01
18:44:12.526 SnapTrack[216:c07] * Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Auto Layout
still required after executing -layoutSubviews. PlayingCell's
implementation of -layoutSubviews needs to call super.'"
I'm trying to run my app (which I designed for iOS7) in the iOS6 simulator. My project contains a TabBarController. In the first view controller in the tabbarcontroller I have a table view containing custom UITableViewCell's. They load without a problem. However, when I switch to another tab, where I have another table view, also with custom cells, I get the error posted above. The viewControllers are set up almost identically (both have subviews organized with auto layout). Has anyone seen this exception/know how to address it?
Thanks!
EDIT: Code for PlayingCell.
#import <UIKit/UIKit.h>
#interface PlayingCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *lblTrackNum;
#property (strong, nonatomic) IBOutlet UIImageView *albumArt;
#property (strong, nonatomic) IBOutlet UILabel *lblSongTitle;
#property (strong, nonatomic) IBOutlet UILabel *lblAlbumTitle;
#property (strong, nonatomic) IBOutlet UILabel *lblArtist;
#property (strong, nonatomic) IBOutlet UIImageView *imgPlay;
#property (strong,nonatomic) NSMutableDictionary *songInfo;
- (IBAction)downloadBtnPressed:(id)sender;
#end
#import "PlayingCell.h"
#implementation PlayingCell
#synthesize songInfo;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)downloadBtnPressed:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[songInfo objectForKey:#"trackViewUrl"]]];
NSLog(#"%#",[songInfo objectForKey:#"trackViewUrl"]);
}
-(void)layoutSubviews
{
[super layoutSubviews];
}

Do what the error states. In one of your custom cell classes you appear to be overriding the layoutSubviews method. You must call [super layoutSubviews] in your implementation. This is typically the 1st line.
- (void)layoutSubviews {
[super layoutSubviews];
// the rest of your custom layout code
}

Related

Storyboard initializing UIView instead of my custom class

I have a custom class called NumberView which subclasses UIScrollView. I put a UIView onto my storyboard and changed the class to NumberView:
I then created an outlet to my ShotTraceViewController class:
#interface ShotTraceViewController ()
#property (weak, nonatomic) IBOutlet NumberView *numberView;
#end
I've defined a few methods in the NumberView.h and NumberView.m:
.h:
#interface NumberView : UIScrollView
#property (weak, nonatomic) id<NumberViewDelegate> number_delegate;
-(void)setTotalBtns:(int)total;
...
#end
.m:
#interface NumberView ()
#end
#implementation NumberView {
}
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
return self;
}
-(void)setTotalBtns:(int)total {
NSLog(#"Setting buttons");
}
#end
But when I try to call [self.numberView setTotalBtns:3]; from my ShotTraceViewController. I get an exception:
-[UIView setTotalBtns:]: unrecognized selector sent to instance 0x104fad3e0
Whoa! That's not right, it's supposed to be a NumberView not a UIView! Am I doing something wrong?
I've also noticed that initWithFrame and initWithCoder are both not being called.
I don't know why this worked, but unchecking the Inherit Module from Target button underneath the class selection fixed everything.

IBOutlet image view redraws itself when selected

I can't figure out what is happening with my custom UITableViewCell.
The header file:
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UIImageView *imageView;
#property (nonatomic, strong) IBOutlet UILabel *personName;
#property (nonatomic, strong) IBOutlet UILabel *detailedTextLabel;
#property (nonatomic, strong) IBOutlet UILabel *amountLabel;
#end
The implementation file:
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {}
return self;
}
There is also a XIB file. The layout is an image view on the left and text and detailed text on the right.
Here is the problem. These custom cells are used in a UITableViewController. When I select one of the UITableViewCell, the imageView would shift to the right instead of being in the center when it initially gets loaded.
The most confusing thing is that the problem goes away if I add #synthesize to the implementation. As far as I know, the synthesize adds setter and getters however, in my implementation file, those properties are not set anywhere. They are an IBOutlet property so that my XIB can connect to them. Please tell me what might be going on?
Thanks!
There's no needs to synthesize the outlets. Seems like there are autolayouts enabled on xib file. Just disable them and try again.

Custom UITableViewCell running another classes delegate methods

I currently have a custom timer class and a custom UITableviewCell, the timer has a set of delegate methods that fire based on the timer state, the custom cell has an instance of this timer class as a property and has the delegate methods set in the M file, however i notice they are not firing when the cell is assigned a timer and the timer is started.. my code is as follows:
TABLEVIEW CELL H
#import <UIKit/UIKit.h>
#import "TimerClass.h"
#interface AeroPressCell : UITableViewCell <timerDelegate>
#property (weak, nonatomic) IBOutlet UILabel *centerLabel;
#property (weak, nonatomic) IBOutlet UILabel *rightLabel;
#property (weak, nonatomic) IBOutlet UILabel *bottomLabel;
#property (weak, nonatomic) IBOutlet UIImageView *cellImage;
#property (weak, nonatomic) IBOutlet TimerClass *timer;
TABLEVIEW CELL M
#import "AeroPressCell.h"
#implementation AeroPressCell
#synthesize cellImage;
#synthesize centerLabel;
#synthesize bottomLabel;
#synthesize rightLabel;
#synthesize timer;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#pragma mark Timer Delegate
#pragma mark -
- (void)timerStarted{
NSLog(#"STARTED");
}
- (void)timerTick{
}
- (void)timerPaused{
NSLog(#"PAUSED");
}
- (void)timerResumed{
}
- (void)timerFinished{
NSLog(#"FINISHED");
}
#end
I know that these delegate methods work as i have tested by using an instance of this timer class in a viewcontroller and it's firing, is this not possible with a uitable view cell? Currently the timers are counting down in each cell but the delegate methods do not fire, any idea whats happening?
In your AeroPressCell code, you haven't set the delegate of the timer class to the cell's instance. Try setting the timer's delegate like so - self.timer.delegate = self.

Objective-c UIScrollView now scrolling nor vertically nor horizontally

I don't understand why my project doesn't work.
I can open the window, but the scrollview wont scroll, which means I can't see the bottom of my UI. Why? I read answers to similar questions but they didn't help me fix my issue.
THIS IS THE DETAILVC.H
#interface DetailVC : UIViewController {
IBOutlet UIImageView *iv_main, *iv_ratings;
IBOutlet UITextField *tf_name, *tf_dob, *tf_age, *tf_weight, *tf_height,*tf_birthplace;
IBOutlet UITextView *tv_description;
IBOutlet UIButton *but_movies, *but_edit;
IBOutlet UINavigationBar *navbar;
IBOutlet UIScrollView *scrollView;
}
#property (nonatomic, retain) IBOutlet UIImageView *iv_main, *iv_ratings;
#property (nonatomic, retain) IBOutlet UITextField *tf_name, *tf_dob, *tf_age, *tf_weight, *tf_height, *tf_birthplace;
#property (nonatomic, retain) IBOutlet UITextView *tv_description;
#property (nonatomic, retain) IBOutlet UIButton *but_movies, *but_edit;
#property (nonatomic, retain) IBOutlet UINavigationBar *navbar;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#end
THIS IS THE DETAILVC.M
#import "DetailVC.h"
#interface DetailVC ()
#end
#implementation DetailVC
#synthesize iv_main, iv_ratings, tf_age, tf_birthplace, tf_dob, tf_height, tf_name, tf_weight, tv_description, but_edit, but_movies,navbar, scrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
scrollView.contentSize = CGSizeMake(320, 1000);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
------Connection Inspector
http://s18.postimg.org/qjzrdz9a1/Schermata_2013_08_07_alle_16_47_19.png
------Scroll View attributed Inspector
http://s8.postimg.org/5eniaq8r9/Schermata_2013_08_07_alle_17_35_04.png
Is there a chance that the frame of your UIScrollView has the same size as contentSize? If yes, the UIScrollView will think it has enough frame to display all of the content.

iOS Instantiate Custom View from Nib File across multiple UIViewController

I need an example of how to instantiate a custom view from a nib file. i wrapped my head around several stackoverflow posts but i didn't get it. Maybe i only need a hint how to do it.
what I want to do is - creating a custom view with interface builder. it should be a template so that i can instantiate it multiple times to add it to multiple view controllers.
So far I have created my custom view xib file called MyIssueView.xib. Which consists in fact just of the main view and some labels.
And I created a Subclass of UIView which is called IssueView with the Outlets for my labels in the MyIssueView.xib.
How do I now hook up the outlets with the IB? And how can I instantiate the IssueView from any ViewController?
Would be glad for an example!
Cheers.
UPDATE:
I now have
IssueView.xib
IssueView.h (UIView Subclass)
IssueView.m
My IssueView.h:
#import <UIKit/UIKit.h>
#interface IssueView : UIView
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#property (weak, nonatomic) IBOutlet UILabel *label3;
#property (weak, nonatomic) IBOutlet UILabel *label4;
#end
My IssueView.m:
#import "IssueView.h"
#implementation IssueView
#end
My ViewController.m:
#import "AllIssuesViewController1.h"
#import "IssueView.h"
#import "UIView+NibLoading.h"
#interface AllIssuesViewController1 ()
#end
#implementation AllIssuesViewController1
- (void) loadView
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
_issueView = [IssueView loadInstanceFromNib];
}
It gives me an error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x8292580> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label1.'
I have a category that extends UIView called UIView+NibLoading.
In it is this code...
#import "UIView+NibLoading.h"
#implementation UIView (NibLoading)
+ (id)loadInstanceFromNib
{
UIView *result;
NSArray* elements = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id anObject in elements) {
if ([anObject isKindOfClass:[self class]]) {
result = anObject;
break;
}
}
return result;
}
#end
Then when I want to instantiate a UIView subclass from a nib I just import the category and I can run...
self.issueView = [IssueView loadInstanceFromNib];\
You need to connect the labels like this...
If you connecting the labels to "File's Owner" then you'll need to remove those connections.

Resources