I have a custom cell designed with a scrollview and a pageview control, which I am displaying as follows
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = #"ScrollViewCell";
cell = (ScrollViewCell*)[newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray * customcellArray = [[NSBundle mainBundle]loadNibNamed:#"ScrollViewCell" owner:self options:nil];
for(id customcellObject in customcellArray){
if([customcellObject isKindOfClass: [UITableViewCell class]]){
cell = (ScrollViewCell *)customcellObject;
break;
}
}
}
// Customize your UIScrollView here..
[cell.scrollView setDelegate:self];
[cell.scrollView setPagingEnabled:YES];
scrollView = cell.scrollView;
pageControl = cell.pageControl;
cell.backgroundColor = [UIColor grayColor];
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...
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor grayColor];
[cell setBackgroundView:bgview];
return cell;
}
The scrollview appears and scrolls just fine in cell , but problem is page control doesnt update with the scroll, basically I want to update the page control on scroll of scrollview but since page control and scrollview both are from cell I am not getting how to achieve this, I tried implementing UIScrollViewDelegate protocol with cell and then the parent view of table view but couldnt get it working, pleas guide.
Thanks
Vishal
Don't place your UIPageControll over UIScrollview in your custom cell, if it is then it will get scroll along UIScrollView itself.So create your Custom cell like this and make outlet for each UIScrollview & UIPageControll,
One change in your cellForRowAtIndexPath
// Customize your UIScrollView here..
[cell.scrollView setDelegate:self];
[cell.scrollView setPagingEnabled:YES];
[cell.scrollView setTag:indexPath.row]; // set indexpath.row as tag for cell.scrollview
NSArray * colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor],[UIColor blueColor],nil];
cell.pageControll.numberOfPages = [colors count];
And in,
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat xOffset = sender.contentOffset.x;
CGFloat frameWidth = sender.frame.size.width;
int page = floor((xOffset - frameWidth / 2) / frameWidth) + 1;
GroupButtonCell * cell = (GroupButtonCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]]; // get appropriate cell based on scrollview tag (sender.tag).
cell.pageControll.currentPage = page; // assigning to pagecontroll
}
Hope this will help..
Related
I have requirement to scroll the images horizontally and vertically.
I have sections which will segregated vertically and in each section itself it will be so many images which will be scrolled horizontally.
I made vertical scrolling using UICollectionView with Ray wenderlich tutorial but how do I get horizontal scrolling in each section?
Should I use any other object like UITableView or UIScrollview for this purpose?
Any tutorial available how to achieve this?
Is it compulsory to implement Layout subclass or UICollectionviewFlowlayout when using collection view?
EDIT
Till now I have implemented this code which scrolls vertically and has 5 sections each but not able to scroll the individual sections
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
NSArray *arrayReturn = [self returnArray:section];
return arrayReturn.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ImageCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"imagecell" forIndexPath:indexPath];
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSArray *arrSection = [NSArray arrayWithArray:[self returnArray:indexPath.section]];
NSString *fileName = [arrSection objectAtIndex:indexPath.row];
UIImage *image = [UIImage imageNamed:fileName];
dispatch_async(dispatch_get_main_queue(), ^{
// cell.imgVw.image = ;
if([CollectionView.indexPathsForVisibleItems containsObject:indexPath]){
ImageCell *currentCell = (ImageCell *) [cv cellForItemAtIndexPath:indexPath];
currentCell.imgVw.image = image;
}
});
}];
[self.thumbnailQueue addOperation:operation];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
HeaderView *Header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"PhotoHeaderView" forIndexPath:indexPath];
Header.lblHeaderTitle.text = #"Header title";
Header.backgroundColor = [UIColor yellowColor];
return Header;
}
Here is a similar tutorial:
http://www.raywenderlich.com/4723/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-2
Basically you need to do following:
Take a tableView or CollectionView
In each cell you need to add a scrollview with horizontal scrolling enabled.
Add items to scroll view of each cell while creating cells and give the scroll view appropriate content size.
It will give you a desired result.
Try this
**viewController.h**
IBOutlet UIScrollView *scrollViewMain;
**viewController.m**
- (void)viewDidLoad
{
[super viewDidLoad];
[self createDisplay];
// Do any additional setup after loading the view, typically from a nib.
}
-(void) createDisplay
{
for(UIView *subView in scrollViewMain.subviews)
{
if(![subView isKindOfClass:[UIImageView class]])
{
[subView removeFromSuperview];
}
}
int yPos =5;
for (int i=0; i< 10; i++)
{
int xPosition=5;
UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(xPosition, yPos, scrollViewMain.frame.size.width, 100)];
scrollView.backgroundColor =[UIColor clearColor];
scrollView.autoresizingMask =UIViewAutoresizingFlexibleWidth;
xPosition +=5;
for (int j=0; j<10; j++)
{
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, 0, 100, 100)];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.backgroundColor =[UIColor greenColor];
lblTitle.text = #"test";
lblTitle.numberOfLines =0;
lblTitle.font = [UIFont boldSystemFontOfSize:15];
[scrollView addSubview:lblTitle];
xPosition +=100;
xPosition +=10;
}
[scrollView setContentSize:CGSizeMake(xPosition, 100)];
[scrollViewMain addSubview:scrollView];
yPos +=120;
}
[scrollViewMain flashScrollIndicators];
[scrollViewMain setContentSize:CGSizeMake(0, yPos)];
}
I have to create a tableview with one of the cell having a scrollview with paging enabled and paging control in it. 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.
I have following code in the 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
#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
You are trying to add customize your UIScrollView in UITableViewCell class itself, i am not sure whether it will work.
instead try like this in your UITableView class 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.
[self.contentView addSubview:scrollView];
You should add scrollview on cell. Check M not sure. I dint got time to try in project.
This code shows how to add a scrollview to a cell and works perfect but I want to load a scrollview which I have designed in XIB and which is paging enabled, I tried directly loading the scrollview as a contentview to cell but it doest appear proper and doesnt scroll horizontally which it does outside the tableview cell, please guide me what changes will be required
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"ScrollCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 25, 320, 25)];
scroller.showsHorizontalScrollIndicator = NO;
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
contentLabel.backgroundColor = [UIColor blackColor];
contentLabel.textColor = [UIColor whiteColor];
NSMutableString *str = [[NSMutableString alloc] init];
for (NSUInteger i = 0; i < 100; i++) { [str appendFormat:#"%i ", i]; }
contentLabel.text = str;
[scroller addSubview:contentLabel];
scroller.contentSize = contentLabel.frame.size;
[cell addSubview:scroller];
}
// cell.textLabel.text = [NSString stringWithFormat:#"cell #%i", indexPath.row];
return cell;
}
I am adding some other UI components to my XIB scrollview at runtime like button etc. code is as below
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];
Please suggest me the modifications with which i can load my designed scrollview to tableview cell
Thanks a lot !
Add All Controls (such like label, scrollView, etc..) in cell.contentView instead of cell addSubview.
I'm trying to implement a UIScrollView in each cell of `UITableView'. I wrote my code based on Apple's custom Scrollview project: https://developer.apple.com/library/ios/#samplecode/Scrolling/Listings/ReadMe_txt.html
Each table cell's UIScrollView scrolls through a set of labels, which are initialized in the cellForRowAtIndexPath method. Each label's text is set after initialization to equal a string from an array.
Everything works correctly until I scroll down to the 4th table cell. The UIScrollView in this cell has the same exact labels as the first cell. The same first 3 set of labels or the first 3 scrollViews keep repeating after the first 3 cells. The strange part is, when I log the label.text after it is set, the output shows the correct label.text of what should be displaying in the respective cell.
- (void)layoutScrollLabels: (float)arrayCount
{
UIView *view = nil;
NSArray *subviews = [cell.popularLinkScrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UILabel class]] && view.tag >= 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
[cell.popularLinkScrollView setContentSize:CGSizeMake((arrayCount * kScrollObjWidth), [cell.popularLinkScrollView bounds].size.height)];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Custom Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *dictionary = [parseDataArray objectAtIndex:indexPath.row];
NSArray *popularLinkTitleArray = [dictionary objectForKey:#"popularLinkTitleArray"];
cell.popularLinkScrollView.clipsToBounds = YES;
kScrollObjHeight = cell.popularLinkScrollView.frame.size.height;
kScrollObjWidth = cell.popularLinkScrollView.frame.size.width;
NSUInteger i;
for (i = 0; i < popularLinkTitleArray.count; i++)
{
NSString *string = [NSString stringWithFormat:#"%#", [popularLinkTitleArray objectAtIndex: i]];
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:#"%#", string];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 5;
NSLog(#"%#", label.text);
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = label.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
label.frame = rect;
label.tag = i; // tag our images for later use when we place them in serial fashion
[cell.popularLinkScrollView addSubview:label];
}
[self layoutScrollLabels:popularLinkTitleArray.count];
}
return cell;
}
You should not add label as a subview to popularLinkScrollView in cellForRowAtIndexPath. If the count of labels is required to be dynamic, try this code before your for loop in cellForRowAtIndexPath.
for (UIView* subView in cell.popularLinkScrollView.subviews)
[subView removeFromSuperview];
I have a UITableView and a UITableCell subclass. Each table cell has two scrollviews that each rotate a dynamic amount of labels, which are created in my table cell implementation. I have poor scrolling performance and I believe it is from memory leaks. I'm referencing this stackoverflow correct answer to fix my problem: cellForRowAtIndexPath memory management.
I can't figure out how to tweak my code so I don't have to allocate memory every time I create labels.
ViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Custom Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *dictionary = [parseDataArray objectAtIndex: indexPath.row];
NSArray *popularLinkTitleArray = [dictionary objectForKey:#"popularLinkTitleArray"];
NSString *soundCloudLink = [dictionary objectForKey:#"soundCloudLink"];
NSArray *soundCloudTrackTitleArray = [dictionary objectForKey:#"soundCloudTrackTitleArray"];
cell.artistNameLabel.text = artistName;
[cell createPopularLinkLabels: popularLinkTitleArray];
return cell;
}
CustomCell.m
- (void)layoutScrollLabelsForPopularLinkScrollView: (float)arrayCount
{
UIView *view = nil;
NSArray *subviews = [popularLinkScrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UILabel class]] && view.tag >= 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
[popularLinkScrollView setContentSize:CGSizeMake((arrayCount * kScrollObjWidth), [popularLinkScrollView bounds].size.height)];
}
-(void) createPopularLinkLabels:(NSArray *) popularLinkTitleArray
{
popularLinkScrollView.clipsToBounds = YES;
kScrollObjHeight = popularLinkScrollView.frame.size.height;
kScrollObjWidth = popularLinkScrollView.frame.size.width;
for (UIView* subView in popularLinkScrollView.subviews)
[subView removeFromSuperview];
NSUInteger i;
for (i = 0; i < popularLinkTitleArray.count; i++)
{
NSString *string = [NSString stringWithFormat:#"%#", [popularLinkTitleArray objectAtIndex: i]];
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:#"%#", string];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 5;
[label setFont:[UIFont fontWithName:#"Calibri" size:18]];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = label.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
label.frame = rect;
label.tag = i; // tag our images for later use when we place them in serial fashion
[popularLinkScrollView addSubview:label];
}
[self layoutScrollLabelsForPopularLinkScrollView:popularLinkTitleArray.count];
}
Are you using ARC? If not, you should autorelease to
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]
Cell objects are reused and you do not need to re-add subviews each time you change the contents. Just change the data and update the label frames if needed.
If you have distinctly different cells, consider using separate custom cell classes. You will need to use a separate value for the reuseIdentifier.
Also consider not creating a custom cell but just adding elements to the standard UITableViewCell. Read this about styling a cell.
The first thing you could do is try to reuse UILabels in createPopularLinkLabels. Right now, you are just removing them before adding more.
But if the number of popular links is not the same in all the cells, then you will have to remove the remaining ones or hide them and there will be no way to avoid memory allocations.
A better solution would be to insert a custom UIView inside your scroll view and draw all the texts manually instead of creating tons of subviews.
Hope this helps,
Thomas