【问题标题】:ScrollView with paging in TableView Cell在 TableView 单元中带有分页的 ScrollView
【发布时间】:2023-03-30 22:57:01
【问题描述】:

我必须创建一个表格视图,其中一个单元格具有启用分页和分页控制的滚动视图。我已经尝试过。

我在 IB 中创建了一个 TableView Cell,里面有两个东西 1.滚动视图 2.页面控件和运行时放置在scrollview每个页面上的一些标签,我已经在普通视图中测试了这个scrollview和分页控件,它可以正常工作。

我在 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

以及 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

我在表格视图中加载这个单元格如下

static NSString *CellIdentifier = @"ScrollViewCell";

ScrollViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[ScrollViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier] autorelease];

}


return cell;

当我运行这个应用程序时,它会显示一个具有所需高度的空表,但其中没有任何内容。 我的方法正确吗?如果是,有什么遗漏?

谢谢 问候 维沙尔

【问题讨论】:

    标签: iphone ios scrollview tableview


    【解决方案1】:

    您正在尝试在UITableViewCell 类本身中添加自定义您的UIScrollView,我不确定它是否会起作用。

    改为在您的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;
    }
    

    【讨论】:

    • 嗨,它至少可以工作,但我仍然无法向它添加页面控件,我会尝试让你知道
    • 我现在的问题是,如果我们在 cellForIndexPath 方法中绘制所有内容,那么 XIB 有什么用?我已将 tabelcell 的背景颜色设置为灰色,但表格中的单元格似乎是白色的,这可能是什么原因?
    • 一个问题,但是目前这个问题是页面控件不会随着滚动而改变,并且 viewdidscroll 函数没有被调用,它似乎已经在 scrollviewcell.m @Vedchi 你能帮忙吗?
    • @vishal 我将我的答案发布到你的新 SO 线程:stackoverflow.com/questions/15910747/… 看看这个。
    【解决方案2】:

    在 ScrollViewCell.m 中试试这个 init 方法内部。

           [self.contentView addSubview:scrollView];
    

    您应该在单元格上添加滚动视图。检查 M 不确定。我没有时间在项目中尝试。

    【讨论】:

    • 试过但没有用,没有出现相同的空白单元格
    • 你也应该分配 init scrollView
    • 好吧,这是我不明白的东西,作为一个新手,你能告诉我确切的可能语法吗?因为滚动视图已经存在于 XIB 文件中
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2014-08-31
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2018-06-16
    相关资源
    最近更新 更多