IBAction on a button in a custom UITableViewCell

Using iOS 5: I have a scenario in which I have to create a tableView with custom cells. Custom cells have a controller called the TainingCellController Subclass UITableViewCell and the NIB TrainingCell.xib file. Although the parent table is placed inside a UIViewController called TrainingController ..

Now I am very interested in the relationship of this CustomCell to a file owner who receives IBActions or IBOutlets.

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

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

And what if I need the outputs for the “tags in the custom cell” that will be defined in the TrainingCellController, and the button action will be defined in the TrainingController?

+4
source share
2 answers

You set the UITableViewCell class to your CustomCell class, and you define the IBoutlet class in CustomCell and connect them.

And then you set your Xib file owner to your ViewController , and in ViewController you declare

 IBOutlet CustomCell *yourClassLevelCell; 

and connect this IBoutlet to your Xib UITableViewCell

now when you initialize the cell inside your ViewController's cellForRowAtIndexPath , you will add the 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 } 
+8
source

Try executing dynamic buttons in the same class as tableView

 -(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 } } 
+1
source

Source: https://habr.com/ru/post/1400958/


All Articles