2014-03-03 31 views
0

我已經創建了一個包含UITabBar屬性的特定列表的自定義單元格。這個單元格有其特定的類叫做RecomandationCell,我已經聲明瞭這些屬性。從自定義UITableViewCell中委託製表符

我用這個自定義單元格來創建多個小區不同的對象,我也想知道,當我在一個特定的行點擊該標籤欄的項目稱之爲來自代表團的<UITabBarControllerDelegate>這是

- (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"); 
    } 
} 
其方法

問題是,這種方法不會被解僱,因爲我不知道在哪裏委託它,甚至如果我委託它,我不知道在特定索引點擊了哪個標籤欄。

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 

Recomandat ionViewController.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 

任何建議,或者如果您需要更多的信息,請不要猶豫,評論

+0

那你有沒有設置單元格的標籤欄的委託? – Wain

+0

我已經設置了另一種方法的cell.delegate = self。 UITableViewCell的類沒有viewDidLoad,我不會在哪裏啓動它。 – EridB

+0

告訴我你想要做什麼..應用程序流。 –

回答

1

OKY u能做到這樣

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"); 
    } 
} 


在此之後ü可以調用自定義的委託方法到控制器作進一步處理

+0

這是我正在尋找的答案,通過將indexPath.row定義爲在TableView中創建的每個標籤欄的標記進行了一些更改 – EridB

0

第一:你已經在爲電池中,當你可能想指定UITabBarDelegate接口指定UITabBarControllerDelegate

第二:創建選項卡時設置委託。您提供的代碼不會顯示此內容。你需要在某處初始化tabbar,然後在其上設置一個委託。

第三:在單元格中設置特定內容的位置將在tableviewcontroller的cellForRowAtIndexPath中。

第四:要獲得抽頭的TabBar項的指數,你可以做的是,在didSelectItem方法:

NSUInteger indexOfTab = [[tabBar items] indexOfObject:item]; 
NSLog(@"Tab index = %u", indexOfTab); 
+0

已編輯的問題,所以指出我必須在哪裏委託? – EridB

+0

您仍在使用UITabBarControllerDelegate ...方法didSelectItem屬於UITabBarDelegate。在創建單元格時,您並未初始化uitabbar。當您可能希望它是包含它的單元格時,您將代理設置爲uitableviewcontroller。兩者都可以工作,但應該分開關注。你可以編輯你的問題,並說明你想要發生什麼,當你點擊一個標籤欄項目?這就是說;我不知道你爲什麼堅持使用UITabBar ... – Moonwalkr