I'm trying to implement a custom UITableViewCell and I'm hoping to access the parent tableView from with the cell subclass. I was in the process of creating a custom init method that allows me to specify the tableview, but came across an error.
I tried declaring an iVar of __weak UITableView *_tableView; however, I was given an error stating it was a duplicate declaration. I looked into the UITableViewCell header file, and sure enough, there is a declaration of
#private
UITableView *_tableView;
This is great as I assume iOS will be setting this for me, however I want to know if I'm allowed to use this, or if this is one of those things that will get my app rejected. There is no documentation on this and I've not found any mention of if anywhere online.
Any ideas?
You're right not to try to access the private ivar. I'd suggest not bothering to pass in the superview, either, but just use what's available to the table cell to find its enclosing tableView
UIView+mytable.h:
#import <UIKit/UIKit.h>
#interface UIView (mytable)
- (UITableView *)mySuperTableView;
#end
UIView+mytable.m:
#import "UIView+dsltable.h"
#implementation UIView (dsltable)
- (UITableView *)mySuperTableView
{
if ( [self isKindOfClass: [UITableView class]] )
return (UITableView *)self;
return [self.superview mySuperTableView];
}
#end
No, you can't access the undocumented private ivar. You either need to add your ivar with a different name or get the table view by getting the cell's superview.
Related
I have a custom UITableViewCell which contains two UIButtons (An upVote button and a downVote button), and a label that is meant to count the number of votes. I am using the Parse framework for my backend.
I cannot figure out how to associate the value of a particular label with the custom UITableViewCell that contains the label so that I can then save it to the Parse backend. How can I reference the indexPath of the cell that contains the button using Swift and then associate it with the label so that I can then save it to Parse?
As always, if you feel there is a better way, please share.
.
Everything can happen in your custom UITableViewCell. The key is to store the parse object as a property of the UITableViewCell in cellForRowAtIndexPath: so you never need to worry about looking up the indexPath. Hook your two UIButtons up and when a button is tapped: update the vote count on the parse object, update the label, save the parse object. Something like this should give you the idea:
#interface CustomTableViewCell
#property (nonatomic, strong) MyParseData *parseObject;
#property (nonatomic, strong) UILabel *voteCountLabel;
#end
#implementation CustomTableViewCell
- (IBAction)upVoteButton:(id)sender {
self.parseObject.voteCount++;
[self updateVote];
}
- (IBAction)downVoteButton:(id)sender {
self.parseObject.voteCount--;
[self updateVote];
}
- (void)updateVote {
self.voteCountLabel.text = [self.parseObject.voteCount description];
[self.parseObject saveInBackground];
}
#end
I think you created a custom cell to do this. So you have to create two methods in the custom cell class that will handle the touch up inside to its delegate (the tableview controller, for example). When delegate is called, it has a reference to the cell touched and you can go on.
For a while now I've had this dilemma on my mind. A cell in UITableView is essentially a view, thus the class for UITableViewCell should take care of view related things (i.e. presentation methods, layout and so on.) and have no business logic inside of it (usually taken care of the controller). But since we don't have a controller for each cell and only a controller for the whole table, I have trouble figuring out where to put my cell-wise logic. Putting it in the cell itself breaks MVC, but putting it in the table controller makes it hard to determine what cell the method is being called from (I prefer writing subclasses for my senders if the view is action based so I can add properties to help me determine what view this is).
For instance I have a cell, that cell has a UIButton inside of it, when the button is pushed a UIPopover appears. Now where do I put the popover presentation code (The presentation appears from one specific cell, therefore I must know which cell it's being called from.)
I'd like to know what other people do in this case and what are their best practices.
If you put the presentation of the popover inside the cell, then it's the best option. Why ?, because this is not logic, this is view related things and because the button who makes this action is inside your cell, then the code should be inside your cell (or you can send message(delegate) to your viewController to show that).
Then what is the logic ? The logic is for example: calculating, date operations, sending things to server. All these should be inside another object that we can call it module or manager.
The controller can exchange messages between all these objects (view - model), but the view and the module should be separated from each other.
Update:
You may want to take a look at Single Responsibility principle
Normally, it's to your View Controller to handle the "filling" logic for your cells. Cells are recipient that you fill each time.
It is even said in prepareForReuse: of UITableViewCell :
The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell.
So indeed, your cells shouldn't hold any logic other than displaying.
If you need logic like button in your cell, you should set a delegate (you create one protocol) to your subclass of UITableViewCell and then hold in your UIViewController the cell logic.
If you cell is unique, I recommend you to define your cell as a static cell (no reuse identifier). And make a strong link to it.
You could subclass UITableView and UITableViewCell. Then, add delegate methods for the button. e.g. tableView:buttonWasPressedForCell: & buttonWasPressedForCell:. The tableView would conform to the cell's delegate and receive the message buttonWasPressedForCell:. Then, the tableView would send the message tableView:buttonWasPressedForCell: to it's delegate, in this case, your controller. This way you know which UITableView and which UITableViewCell the message was sent from.
Example:
ABCTableView.h
#protocol ABCTableViewDelegate <NSObject, UITableViewDelegate>
// You may not need this delegate method in a different UIViewController.
// So, lets set it to optional.
#optional
// Instead of passing the cell you could pass the index path.
- (void)tableView:(ABCTableView *)tableView buttonWasPressedForCell:(ABCTableViewCell *)cell;
#end
#interface ABCTableView : UITableView
// Declare the delegate as an IBOutlet to enable use with IB.
#property (weak, nonatomic) IBOutlet id<ABCTableViewDelegate> delegate;
#end
ABCTableView.m
#implementation ABCTableView
#dynamic delegate;
- (void)buttonWasPressedForCell:(ABCTableViewCell *)cell
{
// Check if the delegate responds to the selector since
// the method is optional.
if ([self.delegate respondsToSelector:#selector(tableView:buttonWasPressedForCell:)])
{
[self.delegate tableView:self buttonWasPressedForCell:cell];
}
}
#end
ABCTableViewCell.h
#protocol ABCTableViewCellDelegate;
#interface ABCTableViewCell : UITableViewCell
// Declare the delegate as an IBOutlet to enable use with IB.
#property (weak, nonatomic) IBOutlet id<ABCTableViewCellDelegate> delegate;
#end
#protocol ABCTableViewCellDelegate <NSObject>
// You may not need this delegate method in a different custom UITableView.
// So, lets set it to optional.
#optional
- (void)buttonWasPressedForCell:(ABCTableViewCell *)cell;
#end
ABCTableViewCell.m
#implementation ABCTableViewCell
- (IBAction)action:(id)sender
{
// Check if the delegate responds to the selector since
// the method is optional.
if ([self.delegate respondsToSelector:#selector(buttonWasPressedForCell:)])
{
[self.delegate buttonWasPressedForCell:self];
}
}
#end
Note:
When you dequeue the cell in tableView:cellForRowAtIndexPath: or add the cell using Interface Builder be sure to set the cell's delegate to the tableView.
E.g.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ABCTableViewCell *cell = (ABCTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.delegate = tableView;
return cell;
}
Usually for tasks like this I assign to cell my viewController as delegate (and define some protocol for it). Also, i keep weak reference to object from which I populate my cell, so on button's action I will forward to delegate (viewController) method like this:
- (void)actionOnCell:(UITableViewCell *)cell fromView:(UIView *)sender withItem:(id)sourceItem;
so in this way, I know where from show my popover, and what information (appropriate to sourceItem) show in it.
EDIT Also, if there multiple controls on cell to avoid duplication of pretty similar methods you can just add one parameter to function mentioned above, and define enum of all possible actions
Create an action handler and a data source for the cell. Have your data source conform to the data source protocol (View Model). Then there is no need for the cell to even know about the data model.
In the interface: TableViewCell
#property (nonatomic, weak) id <SomeTableViewCellActionHandler> actionHandler;
#protocol SomeTableViewCellActionHandler <NSObject>
- (void)cell:(SomeTableViewCell *)cell didReceiveStartButtonAction:(UIButton *)button;
- (void)cell:(SomeTableViewCell *)cell didReceivePauseButtonAction:(UIButton *)button;
- (void)cell:(SomeTableViewCell *)cell didReceiveClearButtonAction:(UIButton *)button;
#end
Implementation
- (void)prepareActionsForControls
{
[self.startButton addTarget:self action:#selector(handleStartButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.pauseButton addTarget:self action:#selector(handlePauseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.clearButton addTarget:self action:#selector(handleClearButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handleStartButtonAction:(id)sender
{
[self.actionHandler cell:self didReceiveStartButtonAction:sender];
}
- (void)handlePauseButtonAction:(id)sender
{
[self.actionHandler cell:self didReceivePauseButtonAction:sender];
}
- (void)handleClearButtonAction:(id)sender
{
[self.actionHandler cell:self didReceiveClearButtonAction:sender];
}
When you create your cell in the View Controller
create an action handler that conforms to the MyTableViewCellActionHandler protocol, pass the action handler the View Controller if it needs to do presentation.
cell.actionHandler = self.tableViewCellActionHandler;
You may also provide a datasource for your cell and pass in a View Model. (MVVM) This will allow you to keep only presentation code in the cell and keep all of your business logic where it belongs. Separation of concerns.
I want to create a self-contained UICollectionView subclass (acting as its own data source and delegate) so that I could load it in different viewControllers. Here's what I have so far:
CustomCollectionView.h
#interface CustomCollectionView : UICollectionView <UICollectionViewDataSource, UICollectionViewDelegate>
#property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
#end
CustomCollectionView.m
#import "SSCalendarView.h"
#implementation SSCalendarView
#synthesize collectionView;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Identifier"];
[self addSubview:collectionView];
}
return self;
}
// Below are UICollectionViewDataSource and UICollectionViewDelegate methods
#end
CustomCollectionView.xib
Contains only one view - UICollectionView. It's class is set to CustomCollectionView
File's Owner's class is also set to CustomCollectionView
File's Owner is UICollectionView's delegate and data source
I understand that I have quite a few things wrong here. But perhaps we could use this as a starting point.
My questions are:
How to implement this sub-class correctly? I want to load the view fully from xib
To begin with, and aside from potential MVC violation (this sub-class would do it all), can a UICollectionView be its own data source and delegate?
If above is possible, how do I correctly create an instance of this subclass to use in my view controllers?
There already exists an object which you can use for this purpose - UICollectionViewController. This can be subclassed and added to any view controller (as a child view controller) and already contains a collection view which it is the datasource and delegate for.
The problems with your current approach are:
As you point out, you're putting too much responsibility on one object by having a view be its own datasource and delegate
File's owner for a xib can't be an object from within the xib. When you load the xib, the object you send to the owner argument is the file's owner. I've no idea what you actually end up with using the code you currently have.
I'm not sure why you insist on using a xib anyway - what does this give you, except the headache of an extra file and the complexity of nib loading? Just create a collection view controller, you can specify the layout and register cells in the init and viewDidLoad methods.
First of all making view to act like view controller is violation of MVC, ask you've said so - so you shouldn't probably do it.
Theoretically it's possible to force view to act as delegate & data source but I wouldn't recommend it.
If you still want to do it - just assign delegate & data source to self:
self.dataSource = self;
self.delegate = self;
and adopt UICollectionViewDelegate and UICollectionViewDataSource protocols in .h file
I've been searching all throughout the internet for assistance, however there has been little to no solutions to my issue at hand. My project that im trying to get a gasp on is somewhat unique (UI is not exactly following the typical norms).
Current Development Enviroment:
xcode 4
storyboards instead of nibs
Below is a diagram of what i am trying to accomplish -- all within a UIView Controller:
UIView is the light grey background
UITableView 1 - this is a static (or it can be dynamic, thats another challenge) UITableview which will hold different numeric
values for calculation
UITableView 2 - this is a UITableview which will hold calculated results every time it is run.
UIImageView 1 - this is a calculated image example (I have that figured out)
Im sure experienced developers are fully aware of my issue, and or what im about to ask. I understand that a static UITableView is required to be in a tableview controller, but I need to display both the UItableView's at the same time which means it has to be within a UIView.
I can make the interface look the way I need it to through the IB however when trying to compile and build I receive the error that requires the UITableView's to be within a UITableViewController and not a UIView Controller. I have seen many examples using a master-detail layout, but the only stipulation is that this UITableview NEEDS to be displayed 100% of the time when in this view.
So basically, I am asking for direction... but a code example never hurt either! Thank you 100x's over!
-Jonathan
UITableViewController is just a specialized UIViewController specially designed to display full screen UITableViews. It is (quite) equivalent to use an UITableViewController subclass or an UIViewController <UITableViewDataSource, UITableViewDelegate> subclass to manage a tableview.
So even if UITableViewController has some more spiecialized behaviors (automatically creates the UITableView if it does not exists, scrolls it automatically to display the keyboard, sets itself as the delegate and dataSource of the unique UITableView it manages, etc), you can use a standard UIViewController to manage a UITableView and be its dataSource to fill it.
That's even a way to manage a tableview that is not taking the full screen (as UITableViewController expects its view property to directly be the UITableView it manages, not a subview of its main view or whatever, and thus expects the UITableView to take the whole screen, contrary to using an UIViewController that has an UITableView as a custom-sized subclass of its view)
So in your case, you can have an UIViewController that has two IBOutlets, one for each tableView, and that unique UIViewController can be the dataSource (and delegate) of both the UITableViews. That's not a problem. Just be careful then in your datasource methods to distinguish if you are returning data for the first or the second UITableView to feed the correct tables each time.
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, retain) IBOutlet UITableView* masterTableView;
#property (nonatomic, retain) IBOutlet UITableView* detailsTableView;
#end
#implementation MyViewController
#synthesize masterTableView = _masterTableView;
#synthesize detailsTableView = _detailsTableView;
// Proper memory mgmt not shown here:
// - don't forget to set self.masterTableView and self.detailsTableView to nil in viewDidUnload
// - and to release _masterTableView and _detailsTableView in your dealloc method
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell;
if (tableView == self.masterTableView)
{
static NSString* kMasterCellIdentifier = #"MasterCell";
cell = [tableView dequeueReusableCellWithIdentifier:kMasterCellIdentifier];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithReuseIdentiier:kMasterCellidentifier] autorelease];
// do some configuration common to all your master cells
}
// configure the rest of your cell for each property that is different from one cell to another
}
else if (tableView == self.detailsTableView)
{
// Do exactly the same principle, but for the cells of your "details" TableView
}
return cell;
}
UPDATE: I decided to start over since I was still in the early stages of this app. I repeated everything and for whatever reason, the custom cell took the second time around. I will keep the old files around to confirm another answer, as I imagine I am not the only one who will have this problem.
I am building a tabbed application that requires custom cells in its table views. I have done this a few times and I always seem to hit a speed bump when hooking up these custom cells. The app booted up fine until I started using the custom cell in my table view controller titled SongsTVC. I am receiving a termination with a reason of:
[<SongsTVC 0x6831330> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key albumLabel.
I am using this tutorial and have used it before (changed a few things for ARC and iOS 5) with success. In fact, the code and IB layout I am using is based off of an already working project I have. I am aware of this error commonly presenting itself when you hook up your outlets to the file's owner and not the cell itself. I am not making this mistake but it is still giving me this error. So far, I have removed the label it has a problem with and even deleted the cell's files entirely in order to start over. Any help would be appreciated.
SongCell.h
#import <UIKit/UIKit.h>
#interface SongCell : UITableViewCell{
}
#property(nonatomic, assign) IBOutlet UILabel *titleLabel;
#property(nonatomic, assign) IBOutlet UILabel *artistLabel;
#property(nonatomic, assign) IBOutlet UILabel *albumLabel;
#end
SongCell.m
#import "SongCell.h"
#interface SongCell ()
#end
#implementation SongCell
#synthesize titleLabel, artistLabel, albumLabel;
#end
SongsTVC.h - Header of the TableViewController
#import <UIKit/UIKit.h>
#interface SongsTVC : UITableViewController{
UINib *cellLoader;
}
#end
SongsTVC.m - Relevant TableViewController methods
#import "SongsTVC.h"
#import "SongCell.h"
#interface SongsTVC ()
#end
static NSString *CellClassName = #"SongCell";
#implementation SongsTVC
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
cellLoader = [UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]];
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SongCell *cell = (SongCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName];
if (!cell)
{
//CRASH ERROR POINTS HERE
NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
// Configure the cell...
cell.titleLabel.text = #"SONG";
cell.artistLabel.text = #"Artist";
cell.albumLabel.text = #"Album";
return cell;
}
Interface Builder
NOTE: The cell identifier has been set to "SongCell" in IB and the file owner is UITableViewController because multiple tables will be using this cell. As long as the view is a table, it should work (it has in the past).
UPDATE: The xib file in XML format has been pasted here.
I had this exact problem today, while making a sample project for another SO answer! It looks like your xib is trying to connect an outlet to your view controller instead of your cell. In your screenshot, the outlets look correctly defined, but occasionally an old outlet definition can get left in the underlying XML and cause this type of crash.
If you've changed the files owner class after connecting some outlets, for example, this could confuse it.
You may be able to find it by opening the xib as "source code", look for the element and check there are only the entries you expect. Perhaps search the XML file for albumLabel as well.
If that doesn't work, you may have to scrap the xib and start again.
Unfortunately, the only solution that I have found was to start over. I did everything exactly as I did before and it worked the second time around. It was quite a chore having to scrap the entire thing and start over but it was the only way I could get it to work. I'm leaving this as the answer unless somebody can figure out what happened. (See original post and its update)
I had a similar issue and was able to solve it by rebuilding the Storyboard/NIB.