atastrophic
atastrophic

Reputation: 3143

IBAction on a button in Custom UITableViewCell

Using iOS 5 : : : I have a scenario where I must create a tableView with Custom Cells. The custom cells have a Controller called TainingCellController Subclass of UITableViewCell and a NIB file TrainingCell.xib . While the parent table is placed inside a UIViewController called TrainingController..

Now I am seriously wondering, the relationship of that CustomCell to the File Owner, who receives the IBActions or IBOutlets..

In the Custom Cell NIB file, I can change the file owner (by default set to NSObject) and also can click on the cell itself and change it's class from UITableViewCell to TrainingCellContrller ..

What should be the appropriate classes for these two options ?? Where should the IBActions & IBOutlets be defined (TrainingCellController or TrainingController)?

And what If I need outlets to "labels in custom cell" to be defined in TrainingCellController while button action to be defined in TrainingController??

Upvotes: 4

Views: 6909

Answers (2)

Sanket Pandya
Sanket Pandya

Reputation: 1095

Try working out with dynamic buttons on the same tableView class

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell == nil) 
    { 
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WorkRequestedCC" owner:self options:nil];
{
        for (id oneObject in nib) if ([oneObject isKindOfClass:[WorkRequestedCC class]])
            cell = (WorkRequestedCC *)oneObject;


    }

    UILabel *Button=[[UIBUtton alloc]initWithFrame:CGRectMake(792, 13, 10, 15)];

    [Button addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:Button];
}

-(void) ButtonClicked
{
    //your code here
     }
}

Upvotes: 1

Adil Soomro
Adil Soomro

Reputation: 37729

You will set your UITableViewCell's class to your CustomCell's class and you will defined IBoutlets in CustomCell class and connect them.

And then you will set your Xib's file owner to your ViewController, and in your ViewController you will declare an

IBOutlet CustomCell *yourClassLevelCell;

and connect this IBOutlet to your Xib's UITableViewCell

now when you will initilize the cell inside your ViewController's method cellForRowAtIndexPath you will add target manually, something like this:

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
   [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
   cell = yourClassLevelCell;
   [cell.button addTarget:self ... ];  
   //button is IBOutlet in your CustomCell class which you will have
   //connected to your Button in xib
}

Upvotes: 8

Related Questions