我创建了一个包含特定属性列表的自定义单元格,包括UITabBar。这个单元格有一个名为RecomandationCell
的特定类,我在这里声明了这些属性。
我使用这个自定义单元格创建了包含不同对象的多个单元格,我也想知道当我在特定行中单击此标签栏的项目时,调用其来自<UITabBarControllerDelegate>
的委托的方法< / p>
- (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");
}
}
问题是这个方法不会被触发,因为我不知道委派它的位置,即使我委托它,我也不知道点击了特定索引的哪个标签栏。
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
如有任何建议或需要更多信息,请随时发表评论
答案 0 :(得分:1)
CustomCell.h
中的
@interface CustomTabedCell : UITableViewCell<UITabBarDelegate>//confirms to delegate
在CustomCell.m
文件中
#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");
}
}
之后你可以调用自定义委托方法给控制器进行进一步处理
答案 1 :(得分:0)
首先:当您可能想要指定UITabBarControllerDelegate
时,您已在单元格的界面中指定了UITabBarDelegate
。
第二:在创建标签栏时设置委托。您提供的代码不会显示此信息。您需要在某处初始化tabbar,然后在其上设置委托。
第三:在单元格中设置特定内容的地方位于tableviewcontroller中的cellForRowAtIndexPath
。
第四:要获取tapped tabbar-item的索引,可以使用didSelectItem
方法执行此操作:
NSUInteger indexOfTab = [[tabBar items] indexOfObject:item];
NSLog(@"Tab index = %u", indexOfTab);