在进入该问题的主要目的之前,KASlideShow是github上的一个库。我正在使用此库来显示图像的幻灯片放映。在进行了很多关于如何实现自定义单元的搜索之后,我发现了以下内容:Is it possible to add UITableView within a UITableViewCell。这是我正在使用的代码:
#import "HomeTableViewController.h"
#import "KASlideShow.h"
#import "CustomTableViewCell.h"
@interface HomeTableViewController ()
@property (strong,nonatomic) IBOutlet KASlideShow * slideshow;
@end
@implementation HomeTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SlideShowCell"];
if(cell == nil)
{
cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SlideShowCell"];
}
[_slideshow setDelay:5]; // Delay between transitions
[_slideshow setTransitionDuration:5]; // Transition duration
[_slideshow setTransitionType:KASlideShowTransitionFade]; // Choose a transition type (fade or slide)
[_slideshow setImagesContentMode:UIViewContentModeScaleAspectFill]; // Choose a content mode for images to display
[_slideshow addImagesFromResources:@[@"adidas.jpg",@"adidas_neo.jpeg"]]; // Add images from resources
[_slideshow addGesture:KASlideShowGestureTap]; // Gesture to go previous/next directly on the image
[_slideshow start];
return cell;
}
@end
#import <UIKit/UIKit.h>
#import "KASlideShow.h"
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic) IBOutlet KASlideShow *slideshow;
#import "CustomTableViewCell.h"
#import "KASlideShow.h"
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
@end
@end
KASlideShow永远不会出现在
UITableViewCell
内,我知道出了点问题,但是我不知道该在哪里修复它。 最佳答案
您为什么不将KASlideShow
添加到ur UITableViewController
的标题视图中?
首先创建该headerView
并向其添加1个视图,并将其子类化为KASlideShow
然后将IBOutlet
幻灯片显示与此视图相关联
然后将KASlideShow
设置添加到ViewDidLoad
您的代码应类似于以下代码
#import "SlideShowTableViewController.h"
#import "KASlideShow.h"
@interface SlideShowTableViewController ()
@property (strong,nonatomic) IBOutlet KASlideShow * slideShow;
@end
@implementation SlideShowTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.slideShow setDelay:5]; // Delay between transitions
[self.slideShow setTransitionDuration:5]; // Transition duration
[self.slideShow setTransitionType:KASlideShowTransitionFade]; // Choose a transition type (fade or slide)
[self.slideShow setImagesContentMode:UIViewContentModeScaleAspectFill]; // Choose a content mode for images to display
[self.slideShow addImagesFromResources:@[@"adidas.jpg",@"adidas_neo.jpeg"]]; // Add images from resources
[self.slideShow addGesture:KASlideShowGestureTap]; // Gesture to go previous/next directly on the image
[self.slideShow start];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 0; // <<-- set your sections count
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0; // <<-- set your rows count in section
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
// Configure the cell...
return cell;
}
并且您的Interface Builder应该看起来像