16
votes

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"

8
Are you working with storyBoard?Vishal Sharma
yeah i am working on storybaordMishal Awan
Check this answer (stackoverflow.com/questions/26561461/…) it is for swift but answer will help you.ChintaN -Maddy- Ramani
@Mishal, Then you are doing wrong thing. you need to give tag to that object. i will post answer regarding it.Vishal Sharma
hoow ??? will you please provide some help or update your answerMishal Awan

8 Answers

28
votes

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.

enter image description here

Then give a tag to your UI object, As this in this reference image.

enter image description here

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.

7
votes

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.

1
votes

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):

  1. Create a new Objective-C Class. Set the subclass to: UITableViewCell Give it an appropriate name (i.e. cell)

  2. 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!

0
votes

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.

0
votes

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.

0
votes

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.

0
votes

@Mishal Awan

If you really want to do that and your have a finite number of cells. U can :

  1. Changing your ViewController to be a subclass of UITableViewController, then drage a Table View Controller file to your StoryBoard
  2. Changing the content of Table View in your StoryBoard from Dynamic Prototypes to Static Cells
    1]
  3. Then you can add some views to your cell, for example some labels
    2]

  4. 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

0
votes

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.