Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting IBOutlets to UITableViewCell prototype

I am creating a UITable with custom UITableViewCell prototype. My cell contains UIImageViews, UILabels and UIButtons.

When I control and drag from my buttons to my class' interface it works just fine. However, it does not work with outlets.

When I create the IBOutlet in the .h file, I can only connect if I select the UITable not the cell and of course the result is a broken app.

Do you guys have any idea how to solve this? I do not want to use a custom class just for the cell. I really would like to stick with Storyboard and prototypes.

Thanks in advance

like image 971
Camus Avatar asked Dec 09 '22 12:12

Camus


1 Answers

Using Labels with tag will get the job done but never a good practise ... the best way is to create a custom class of UITableViewCell.

ie, select

New File>Cocoa Touch >Objective C Class

and Create it as a subclass of UITableViewCell now you will get .h and .m files..

Next step is to create the view of the cell for this

select

New File> User Interface > Empty

and now create this with same name of your customcell class (lets say "CustomCell")

now you will have three files CustomCell.h,CustomCell.m,CustomCell.xib

now select the xib file and add UITableViewCell object on the xib and set its custom class as "CustomCell"

Look below pic enter image description here

now after this you can drag any thing (UIImageView,UITextfield,UIButton) to the below view and give outlets onto the CustomClass and manage the actions using delegate methods..

if you have imageView outlet as titleImage ..then you can access the same by creating the cell object in CellForRowAtIndex (TableView delgate method) to set image.

cell.titleImage=[UIImage ImageNamed:@"goo.png"]; 

now one more thing i have to say is that you have to implement an init method also in CustomCell.m to load the nib>>

it will look like the below code.

    -(id)initWithDelegate:(id)parent reuseIdentifier:(NSString *)reuseIdentifier
    {
        
        if (self = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])
        {
            self=(CustomCell*)[[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil] lastObject];
        }
        
        self.backgroundColor = [UIColor clearColor];
        self.backgroundView = NULL;
        self.selectedBackgroundView =NULL;
            
//If you want any delegate methods and if cell have delegate protocol defined
self.delegate=parent;
    
//return cell
    return self;
    }

Now it is better to have delegates if you are using buttons on your cell

so that in the button action method you can call delegate method (pass cell object) and implement the delegate in your ViewController with TableView

here is the example

enter image description here

now you can use your cell for UITableView to populate...and dont for get to set reuseIdentifier Value in CustomCell.xib (same as you set CustomClass)

lets set it namely ,hmm what else "customCell"

so while populating the tableView use

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier=@"customCell";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil)
         cell= [[CustomCell alloc] initWithDelegate:self reuseIdentifier:cellIdentifier];
       
//set cell properties
   cell.titleImage=[UIImage ImageNamed:@"title.png"];

    
    
    return cell;
}

also dont forget to add delegate methods

give

ViewController:UIViewController<CustomCellDelegate>

on your ViewController.h file of ViewController

and then implement its body in your ViewController.m (implementation file)

as

  -(void)cellButtonPressed:(CustomCell*)cell
    {
NSIndexPath *indexPathOfPressedCell = [self.tableView indexPathForCell:cell];
    NSLog(@"Pressed");
    }

This will look like a long method but it's quite useful and readable...

-------NB----------:

Also, return the CustomCell height by implementing

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{}

it may occur....

like image 66
Raon Avatar answered Dec 11 '22 08:12

Raon