[英]ScrollView with paging in TableView Cell
I have to create a tableview with one of the cell having a scrollview with paging enabled and paging control in it. 我必须创建一个tableview,其中一个单元格具有一个滚动视图,其中启用了分页和分页控制。 I have already tried following. 我已经尝试过了。
I have created a TableView Cell in IB which has two things in it 1.ScrollView 2.Page control and some labels placed at runtime on each page of scrollview, I have tested this scrollview and paging control in normal view and it works as expected. 我在IB中创建了一个TableView Cell,里面有两个东西:1.ScrollView 2.Page控件和在scrollview的每个页面上运行时放置的一些标签,我在普通视图中测试了这个scrollview和分页控件,它按预期工作。
I have following code in the ScrollViewCell.h 我在ScrollViewCell.h中有以下代码
#import <UIKit/UIKit.h>
@interface ScrollViewCell : UITableViewCell<UIScrollViewDelegate>
{
UIPageControl* pageControl;
UIScrollView* scrollView;
BOOL pageControlBeingUsed;
}
@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl* pageControl;
- (IBAction)changePage;
@end
And following code in ScrollViewCell.m 并在ScrollViewCell.m中跟随代码
#import "ScrollViewCell.h"
@implementation ScrollViewCell
@synthesize scrollView;
@synthesize pageControl;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [UIColor grayColor];
[self.scrollView addSubview:subview];
// add buttons
CGRect Frame= CGRectMake(frame.origin.x,frame.origin.y,200,50);
//set your x and y position whatever u want.
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.frame = Frame;
UIImage *Img = [UIImage imageNamed:@"second.png"];
[Button setBackgroundImage:Img forState:UIControlStateNormal];
[scrollView addSubview: Button];
// add text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x,frame.origin.y+60,200,50)];
label.textAlignment = UITextAlignmentCenter;
label.text = @"Here you write your text";
label.textColor = [UIColor blackColor];
[scrollView addSubview:label ];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page isvisible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
@end
I am loading this cell in the table view as follows 我在表视图中加载此单元格如下
static NSString *CellIdentifier = @"ScrollViewCell";
ScrollViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[ScrollViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
When I am running this app it shows an empty table with desired height but nothing in it. 当我运行这个应用程序时,它显示一个具有所需高度但没有任何内容的空表。 Is my approach correct ? 我的方法是否正确? if yes whats missing ? 如果是的话什么都没有?
Thanks Regards Vishal 谢谢Vishal
You are trying to add customize your UIScrollView
in UITableViewCell
class itself, i am not sure whether it will work. 您正在尝试在UITableViewCell
类中添加自定义您的UIScrollView
,我不确定它是否可行。
instead try like this in your UITableView
class cellForRowAtIndexPath
, 而是在你的UITableView
类cellForRowAtIndexPath
尝试这样的,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"GroupButtonCell";
CustomCell * cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray * customcellArray = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
for(id customcellObject in customcellArray){
if([customcellObject isKindOfClass: [UITableViewCell class]]){
cell = (CustomCell *)customcellObject;
break;
}
}
}
// Customize your UIScrollView here..
[cell.scrollView setDelegate:self];
[cell.scrollView setPagingEnabled:YES];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor],[UIColor blueColor], nil];
cell.scrollView.contentSize = CGSizeMake(cell.scrollView.frame.size.width * colors.count,cell.scrollView.frame.size.height);
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = cell.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = cell.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[cell.scrollView addSubview:subview];
}
// Configure the cell...
return cell;
}
try this in ScrollViewCell.m Inside init method. 在ScrollViewCell.m中尝试这个内部的init方法。
[self.contentView addSubview:scrollView];
You should add scrollview on cell. 您应该在单元格上添加scrollview。 Check M not sure. 检查M不确定。 I dint got time to try in project. 我有时间尝试项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.