Search code examples
iosobjective-cuitableviewdelegatesuitabbar

Delegate TabBar from custom UITableViewCell


I have created a custom cell with a specific list of properties, including UITabBar. This cell has its specific Class called RecomandationCell, where I have declared these properties.

I use this custom cell to created multiple cells with different objects, also I want to know when I click an item of this tab bar in a specific row call its method that comes from Delegation of <UITabBarControllerDelegate> which is

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag==0)
    {
        NSLog(@"Likes Clicked");
    }
    if (item.tag==1)
    {
        NSLog(@"Comments Clicked");
    }
    if (item.tag==2)
    {
        NSLog(@"Shares Clicked");
    }
    if (item.tag==3)
    {
        NSLog(@"Add Clicked");
    }
    if (item.tag==4)
    {
        NSLog(@"Ratings Clicked");
    }
}

The problem is that this method won't be fired because I don't know where to delegate it, and even If I delegate it I don't know which tab bar at a specific index was clicked.

This image shows graphically what I am trying to do

RecomandationCell.h

#import <UIKit/UIKit.h>


@interface RecomandationCell : UITableViewCell <UITabBarControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *wineImage;
@property (weak, nonatomic) IBOutlet UITextView *wineName;
@property (weak, nonatomic) IBOutlet UILabel *wineYear;
@property (weak, nonatomic) IBOutlet UITabBar *socialTabBar;

@end

RecomandationCell.m

#import "RecomandationCell.h"

@implementation RecomandationCell
@synthesize socialTabBar;

- (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
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag==0)
    {
        NSLog(@"Likes Clicked");
    }
    if (item.tag==1)
    {
        NSLog(@"Comments Clicked");
    }
    if (item.tag==2)
    {
        NSLog(@"Shares Clicked");
    }
    if (item.tag==3)
    {
        NSLog(@"Add Clicked");
    }
    if (item.tag==4)
    {
        NSLog(@"Ratings Clicked");
    }
}
@end

RecomandationViewController.h

#import <UIKit/UIKit.h>
#import "RecomandationCell.h"

@interface RecomandationViewController : UIViewController <UITableViewDataSource,UITableViewDataSource,RecommandationCellDelegate>

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentView;
@property (weak, nonatomic) IBOutlet UITableView *winesTable;

@end

RecomandationViewController.m

#import "RecomandationViewController.h"
#import "RecomandationCell.h"

@interface RecomandationViewController ()

@end

@implementation RecomandationViewController
{
    NSMutableArray *rowArray;

    //Titles for wine properties
    NSMutableArray *wineNames;
    NSMutableArray *wineProductors;
    NSMutableArray *winePlaces;
    NSMutableArray *wineYears;
    NSMutableArray *wineRatings;

    //Badges for Tab Bar items
    NSMutableArray *nrOfRatings;
    NSMutableArray *nrOfLikes;
    NSMutableArray *nrOfComments;
    NSMutableArray *nrOfShares;
    NSMutableArray *addToWishLists;

}
@synthesize winesTable,segmentView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    rowArray = [[NSMutableArray alloc]initWithObjects:@"picture1.jpg",@"picture2.jpg",@"picture3.jpg",@"image4.jpeg",@"image5.jpeg",@"image6.jpeg", nil];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
//    return rowArray.count;
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"mainCell";


    RecomandationCell *cell = [self.winesTable dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.delegate = self;

    cell.wineImage.contentMode = UIViewContentModeScaleAspectFill;
    cell.wineImage.image = [UIImage imageNamed:[rowArray objectAtIndex:indexPath.row]];

    [[cell.socialTabBar.items objectAtIndex:0]setBadgeValue:@"2"];
    [[cell.socialTabBar.items objectAtIndex:1]setBadgeValue:@"3"];
    [[cell.socialTabBar.items objectAtIndex:2]setBadgeValue:@"4"];
    [[cell.socialTabBar.items objectAtIndex:4]setBadgeValue:@"19"];


    cell.wineYear.text = @"2014";
    cell.wineName.text = @"Alex\nMandi\nTirana\nOK";



    return cell;
}


@end

Any suggestions or if you need more information don't hesitate to comment


Solution

  • oky u can do like this

    in CustomCell.h

     @interface CustomTabedCell : UITableViewCell<UITabBarDelegate>//confirms to delegate
    

    in CustomCell.m file

      #import "CustomTabedCell.h"
    
      @implementation CustomTabedCell
    
      - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
     {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
        // Initialization code
    
        UITabBarItem *firstItem = [[UITabBarItem alloc]initWithTitle:@"1" image:nil tag:10];//you can specify image hear i put nil 
        UITabBarItem *secondItem = [[UITabBarItem alloc]initWithTitle:@"2" image:nil tag:11]; // ... give tag number
        UITabBar *tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, 0, 320, 70)]; //leave this if u are added in xib
        tabBar.delegate = self; //delegate to custom cell itself
        tabBar.tag = 100;//leave
        tabBar.items = [NSArray arrayWithObjects:firstItem,secondItem, nil];//add to tab bar
        [self.contentView addSubview:tabBar];
    
       }
      return self;
     }
    
      //in custom cell u are getting the delegate call's
    
     - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
      {
        if (item.tag==10) //compare using tages as u added to tabed bar
         {
            NSLog(@"Likes Clicked");
         }
         if (item.tag==11)
        {
           NSLog(@"Comments Clicked");
        }
        if (item.tag==12)
        {
           NSLog(@"Shares Clicked");
        }
        if (item.tag==13)
       {
          NSLog(@"Add Clicked");
       }
       if (item.tag==14)
       {
           NSLog(@"Ratings Clicked");
       }
     }
    


    after this u can call custom delegate method to controller for further processing