Why Label Outlets cannot be connected to UITableViewCell Class - ios

I create a table view cell subclass and set it as the class of the prototype.I want to add the outlets to that class and connect them.
But xcode do not respond.
And i try to add the outlets of button in the next time,it works.
I want to display a picture,it is pity that i don't have enough reputations.
I don't understand why this happen.Can somebody help me,Plesae!!

Create a xib file. Delete UIView and drag UITableviewcell.
Set your UITableviewcell class and a identifier for the cell
Drag the components on the cell and connect them with your class.
On the method (void)viewDidLoad of your UITableViewController, set:
[self.tableView registerNib:[UINib nibWithNibName:nameOfXibFile bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:identifierOfCell];
On the method (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, set:
YourClassTableViewCell *cell = [tableView dequeueReusableCellWithReuseIdentifier:identifierOfCell forIndexPath:indexPath];
and set the values for the labels cell.label.text = #"Your text".
Good luck!

You need to create a subclass for Custom UITableViewCell. Than After you can connect Label outlets.
Check out this great tutorial for creating custom UITableViewCell.
Custom UITableViewCell

To insert referencing outlet by drag you need to do it between #interface and #end statements, like a property (it is actually a property).
You can add a button outlet just because buttons have an action outlets, which are methods.

Related

How should I implement a custom table view cell?

I am trying to change a cell in a tableview from generic to custom and have created a new tableview cell class. However, I cannot get the cell to recognize the new custom cell. Instead, it is still displaying the generic cell although I've changed the setting in storyboard to custom in the identify inspector and also changed the class for the tableview cell to the new custom one. I have also wired the elements in the cell in storyboard to the new custom class.
My understanding is the tableview VC knows which class to use from the tableView cellForRowAtIndexPath method and I have changed that too with the code below. The project compiles but continues to show old generic cell. Can anyone see error or suggest what else to do?
#import "customCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
customCell *cell = (customCell *)[self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
Items *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
return cell;
}
Thanks in advance for any suggestions.
Edit:
A custom cell is created in the storyboard on top of tableview by dragging uielements. Maybe this is an issue:
Style of cell is set to Custom:
You need to create new UITableViewCell and enable the "create XIB file...".
Create your custom cell in the XIB and make it's custom class as your class you just created.
Then in tableView cellForRowAtIndexPath register the Nib: (it's in swift but I bet you can figure out the objective c parallel...)
tableView.registerNib(UINib(nibName: "YourUITableViewCellClass", bundle: nil), forCellReuseIdentifier: "YourCustomIdentifierFromTheCustomClass")
Then access your cell:
var cell = tableView.dequeueReusableCellWithIdentifier("YourCustomIdentifierFromTheCustomClass")
and that's it.
you can create custom cell in your main story board by drag the cell view.
create custom class for that prototype.
mention the class name in identifier inspector.
import that class to your view controller .h file.
and made connection of your custom from main story board to custom class.h file.
it works!.
When you create a custom cell you have to do these things:
Setup the labels, images etc on storyboard cell.
Create a custom cell class (inheriting from
UITableViewCell)(CustomCell.h & .m), CustomCell.h having all of the
properties as iboutlet for labels, images etc. and synthesize them all
in implementation.
After creating this custom class go back to storyboard, select the
custom cell, change its class to the CustomCell and give it some
identifier like "MyCustomCell", then right click on the custom cell
and connect the IBOutlets with labels etc.
Now import CustomCell class in the class where you are implementing
the UITableView and use the properties you defined in CustomCell
class.
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString *MyIdentifier = #"MyCustomCell";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://example.com/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
cell.myCustomLabel.text = #"My Text";
return cell; }
Select the cell on your Storyboard, and enter the name of your class in this field.
When you dequeue it from the identifier, it'll be an instance of that class.
Please note that each regular tableViewCell has a builtin and hidden UIImageView.
In Objective-C, you simply do this within your cellForRowAtIndexPath,
cell.imageView.image = [UIImage imageNamed:#"My awesome image"];
In Swift, it's pretty close.
cell.imageView?.image = UIImage(named: "My awesome image")

Outlets cannot be connected to repeating content iOS 5

I am new to iOS i am working on uitableview cell i had drag and drop the button to table view cell from objects inspector. but when i am making its IBoutlet it is showing an error like "Outlets cannot be connected to repeating content" what does it means?? we cant make outlets of UIbutton in tableview cell.kindly review it . i am stuck in it.i am making an outlet like this:
#property (weak, nonatomic) IBOutlet UIButton *sa;
and the error is "The sa outlet to the UIbutton is invalid"
Your answer is that you use an object in UITableViewCell by reusing it,
So you can't create it's outlet except you create Your class for your "UITableViewCell".
Below is a reference for you,
I hope that it's useful for you.
You need to drag and drop the object in your UITableViewCell,
Then you have to give tag for that object.
then you can use it with its tag.
First give an identifier to UITableViewCell,
Below is a reference image for it.
Then give a tag to your UI object,
As this in this reference image.
Below is sample code for you which I use regularly,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *Cell = [self.TableListRegion dequeueReusableCellWithIdentifier:#"List"];
UIButton *objectOfButton = (UIButton *)[CellLast viewWithTag:200];
[objectOfButton addTarget:self action:#selector(YourSelector:) forControlEvents:UIControlEventTouchUpInside];
return Cell;
}
Now you can receive that button event by,
-(IBACTION)YourSelector:(id)sender{
// Your Button in Cell is selected.
// Do your stuff.
}
Feel free to ask if you need more help regarding it.
You can create subclass for UITableViewCell like below code
Create new class named CCell.h and CCell.m
in CCell.h
#interface CCell : UITableViewCell
#property(nonatomic,strong)IBOutlet UILabel *lblTemp;
#property(nonatomic,strong)IBOutlet UIButton *btnTemp;
#end
Now in your Viewcontroller.h
#import "CCell.h"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CCell *cell = (CCell *)[tblView dequeueReusableCellWithIdentifier:#"CCell"];
cell.lblTemp.text = #"asd";
cell.btnTemp.tag = indexPath.row;
[cell.btnTemp addTarget:self action:#selector(btnTempClicked:) forControlEvents:UIControlEventTouchUpInside]
return cell;
}
-(void)btnTempClicked:(UIButton *)btnTemp
{
NSLog(#"Button Clicked Index = %d",btnTemp.tag);
}
Now open your Xib > tap on your UITableviewCell > open right side navigator > open 3rd tab named (Custom Class) > add Class = CCell > now open last tab you will get lblTemp bind option.
Maybe this will help you.
Since you are creating a custom cell, you need to create a class for it. You will subclass UITableViewCell.
For example (using the property that you had in your question):
Create a new Objective-C Class. Set the subclass to: UITableViewCell
Give it an appropriate name (i.e. cell)
In your cell.h file:
Create your property: #property (weak, nonatomic) IBOutlet UIButton *sa;
In the cell.m file:
#synthesize sa = _sa;
- (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
}
3.In Interface Builder, go back to the row that you created.
Click the "Show Identity Inspector".
Under "Custom Class", set that to your cell file.
4.Hold down the "Option" key and click the "cell.h" file. Then connect the button to the IBOutlet.
5.In your table view controller file:
import your cell.h file.
In cellForRowAtIndexPath method:
Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
That's it!
Everything should work then. Hope this helps!
You can't, because the button is part of a cell, and there will (possibly) be multiple instances of that cell when the app runs.
Assume that you can make the connection, and there are 2 cells (and thus 2 buttons) when the app runs, Cocoa Touch can't decide which button will be referenced by that only outlet (neither can you).
You can subclass the UITableViewCell, ask the table view to use your subclass for its cells, and connect the button to the outlet in the subclass. In that case there will be multiple instances of the subclass, and each instance will map to one cell.
Whenever you need want to create custom tableView cell it is recommended to subclass UITableViewcell and add UIButton in that class. And in your tableview delegate method cellForRowAtIndexPath you can create an object of subclassed tableviewCell for every cell.
I have done in UITableviewcell cell's label's outlet connection in UIViewController,it should changed to Create a CustomCell in Subclass of UITableviewcell,then done in outlet connection in this subclass that error cleared.
#Mishal Awan
If you really want to do that and your have a finite number of cells. U can :
Changing your ViewController to be a subclass of UITableViewController, then drage a Table View Controller file to your StoryBoard
Changing the content of Table View in your StoryBoard from Dynamic Prototypes to Static Cells
Then you can add some views to your cell, for example some labels
Connectting the label to your ViewController and continue the remaining work
If you want to create a reusable cell, forget what i have said above
A very simple solution is:
Just take the view or NSLayoutConstraint reference outlets in the subclass of table view cell instead of table view controller and access using object of table view cell in cellForRowAtIndexPath method or any other method.
Note: This is a repetitive object so you can't take reference in table view controller.

Error when trying to use custon UITableViewCell "this class is not key value coding-compliant for the key..."

I have seen the other questions about cleaning up the connections in the nibs, but I don't believe that is my problem.
Currently I'm importing a custom UITableViewCell class, registering the nib in the VC with the UITableView in that VC's viewDidLoad as follows:
static NSString *cellIdentifier = #"cell";
UINib* cellNib = [UINib nibWithNibName:#"[nib for the custom cell]" bundle:[NSBundle mainBundle]];
[_theTableView registerNib:cellNib forCellReuseIdentifier:cellIdentifier];
Then later in the VC I implement the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = #"cell";
CustomTableCell* cell = [tableView dequeueReusableCellWithIdentifier:outgoingNoteIdentifier forIndexPath:indexPath];
return cell;
}
If I don't connect any of the properties in the custom cell (just labels and buttons), then it loads fine, and I can see the images associated with both the labels and the buttons. When I try to connect just one of the properties in the xib file though, I get the above mentioned error.
it sounds like you are connecting your Nib outlets to the file's owner.
Try connection them directly to the cell instead.
I usually follow this flow:
Set file's owner to your custom cell class
Add outlets to file's owner, to get generated properties
Set cell's Custom Class to be same as the one specified as file's owner
Add all outlets to the cell. XCode will now show the properties your created in step 2
Happy coding

Reusable UITableView's prototype cell

I want to create a prototype cell which can be used in different table view via the storyboard.. What is the right way to do this? Any pointers appreciated.
I don't think you can create a prototype cell and share it between tables in a storyboard, but you can create a prototype cell in a nib and then load that in the ViewDidLoad method and then use that in your table view. It is really quite simple, here is how...
A. add the nib file:
1. Select New File...
2. Select IOS -> User Interface
3. Select "Empty" -> this will add a new file .xib file to your project
4. Drag a UITableViewCell from the object browser into your xib file and customize to your liking
5. Use the Utilities pane to change properties -> editing a nib is very
similar to editing a storyboard.
6. Make sure you name your cell - I chose the name cellFromNib, but you will probably want something else.
B. Load the UITableViewCell in each table's viewDidLoad method:
- (void)viewDidLoad
{
// load the cell in the nib - the nib can only contain this one UITableViewCell
[self.tableView
registerNib:[UINib nibWithNibName:[self #"nibFileNameWithoutExtension"]
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:[self #"cellFromNib"]];
}
C. De-queue the nib's tableViewCell...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellFromNib" forIndexPath:indexPath];
// customize your cell here...
}
D. Add a "dummy" prototype cell to your TableView in your storyboard.
Make a segue from this "dummy" cell to the view you want displayed when the cell is selected - make sure to name the segue - I'll call it #"theSegue" for this example. You will reference this segue in your code.
E. Finally, add code to segue from that cell...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is basically what the storyboard does under the hood...
// make sure you have a "dummy" prototype cell with this segue name for each
// tableview that uses this cell
[self performSegueWithIdentifier:#"theSegue" sender:self];
}
If you want to specialize your cell code, create a class that subclasses UITableViewCell
I think that is everything you need.
I would say do not be afraid of doing something like this because, if you are serious about IOS programming, you will learn something new. It really does make for much better reusable code.

UITableView not displaying content in UIViewController

I have added a UITableView inside a UIViewController in IB, I have set the TableView content to "Static Cells" since that's what I want, and everything seems fine when I haven't added any content to the UITableView. But if I for example change the first UITableViewCell's background color to black it doesn't display black when running the app. I have added UITableViewControllerDelegate and UITableViewDataSource and set the tableView.delage = self;. But still no changes I make to the tableView displays when I run the app.
What can the problem be?
NOTE: I have also tried to add tableView.dataSource = self;, but that just make the app crash.
Yes, you can have static table view content in UIViewController.
All you need to do is:
-Create the table's static cells in interface builder and design them the way you like.
-Make the UIViewController implement table view's data source and delegate:
#interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
-Connect the table view's delegate and dataSource to the view controller in interface builder
-Implement -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section to return the number of your cells. (e.g. return 10, yes simple as that)
-Connect your cells to your code as IBOutlets in Interface Builder. IMPORTANT: Make sure they are strong, weak won't work. e.g. #property (strong, nonatomic) IBOutlet UITableViewCell *myFirstCell;
-Implement -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to return the correct cell at index path. e.g:
int num = indexPath.row;
UITableViewCell *cell;
switch (num) {
case 0:
cell = self.myFirstCell;
break;
case 1:
cell = self.mySecondCell;
break;
}
return cell;
If you apply all these steps, you should have working static cells that works for tables with not many cells. Perfect for tables that you have a few (probably no more than 10-20 would be enough) content. I've ran the same issue a few days ago and I confirm that it works. More info check here: Best approach to add Static-TableView-Cells to a UIViewcontroller?
You will want to use a UITableViewController, not a UIViewController with a UITableView added to it, because you're only supposed to use static cells with a UITableViewController. There are probably ways to hack around it so you can get the static cells to work, but it's much simpler to just use a UITableViewController, and you'll have fewer issues to deal with, especially if you ever change the content of the table.
Seems you have problem with the background issue for UITableViewCell. So don't use background for checking if content is drawing or not.
You can use debugger for this for example or NSLog.
NOTE: the cell has content view that can be modified. I don't remember but seems the cell has not got background property that can be adjusted with a color.
If you tried this line below e.g. - it will no work and color will be white as default color.
[cell setBackgroundColor:[UIColor blackColor]];
Try to add something to the cell for example picture and then you can see the result as I think.
Use this code:
[cell.contentView setBackgroundColor:[UIColor blackColor]]; in this delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
it will help you as I think.
Have you implementede the protocol? ...
another thing is that when implementing the protocol i had an issue when no cell was displayed..
try with this implementation for the given method.
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier=#"Cell";
CobranzaCell *cell = [[CobranzaCell alloc]init];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
if (cell == nil) {
cell = [[CobranzaCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
return cell;
}
You cannot use the static cells in simple UIViewController subclass. Instead, you should use the instance of UITableViewController. The hack is in your storyboard drag the instance of UIViewController to your storyboard canvas. Then, drag the Container View from objects library and drop it to your UIViewController's view. By default it will create the embed segue with related UIViewController instance. What you want to do - delete this view controller, drag and drop instance of UITableViewController from objects library, then right click and drag from your Container View to just dropped UITableViewController. Chose embed. Now your table view controller gets the size of container view and you can use static cells in it! Hope it will help!

Resources