I have double items in my collectionView. How can I fix it?
I have UITableViewCell with UICollectionViewCell inside it.
I set elements size and direction based on its count.
My cell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.layout = [[UICollectionViewFlowLayout alloc] init];
self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
self.layout.itemSize = CGSizeMake(70, 70);
self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = [[IndexedCollectionView alloc] initWithFrame:CGRectMake(10, 250, 60, 320) collectionViewLayout:self.layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
[self.contentView addSubview:self.collectionView];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.collectionView.frame = self.contentView.bounds;
}
-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate index:(NSInteger)index
{
self.collectionView.dataSource = dataSourceDelegate;
self.collectionView.delegate = dataSourceDelegate;
self.collectionView.index = index;
[self.collectionView reloadData];
}
Delegate Methods
-(FeedCell *)collectionView:(IndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FeedCell *cell = (FeedCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = collectionView.frame;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
NSInteger count = [self test:collectionView].count;
if (count == 0)
{
return nil;
}
NSInteger imageIndex = 0;
if (count == 1)
{
[imageView setFrame:CGRectMake(0, 110, 310, 200)];
imageIndex = indexPath.item;
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
if (count == 2)
{
[imageView setFrame:CGRectMake(0, 110, 150, 200)];
imageIndex = indexPath.item;
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
if (count == 3)
{
[imageView setFrame:CGRectMake(10, 100, 90, 90)];
imageIndex = indexPath.item;
}
if (count == 4)
{
[imageView setFrame:CGRectMake(10, 100, 130, 130)];//2lines
imageIndex = indexPath.item;
}
if (count == 5 || count == 6)
{
[imageView setFrame:CGRectMake(10, 100, 90, 90)];//2lines
imageIndex = indexPath.item;
}
if (count > 6)
{
[imageView setFrame:CGRectMake(10, 100, 100, 100)];//2lines
imageIndex = indexPath.item ;
//cell.layout.itemSize = CGSizeMake(50, 50);
}
imageView.image = [DataManager imageFromPath:[self test:collectionView][imageIndex]];
[cell.contentView addSubview:imageView];
return cell;
}
The way you create the collectionView Cell in the table view Cell is not correct. Here are some references you can refer to create a collection view.
Collection View Cell Apple Reference.
Create Collection View without Storyboard
Refer to my answer here. You need to subclass your cells.
What is happening is you are dequeuing an existing cell, and then adding in another subview over it. Most of the users on here argue that the ONLY way to fix this is to go through the main subclassing method. It is faster and more "formal", however, you can add another tool to your toolbox:
Instead, you can test if the subview has already been added WITHOUT subclassing, and just stick it in a logic block. Here is what you can do:
BOOL doesContain = [cell.subviews containsObject:imageView];
if (!doesContain)
{
[cell.contentView addSubview:imageView];
}
imageView.image = [DataManager imageFromPath:[self test:collectionView][imageIndex]];
Related
I am trying to create a CollectionView header having horizontal header section but the problem is the section that is being created is vertical and I want everything to be dynamic.
And for this purpose i am using viewForSupplementaryElementOfKind :-
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
DepartmentCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
//UILabel *label = [[UILabel alloc] init];
//label.tag = indexPath.row;
headerView.officeName.text=[NSString stringWithFormat:#"%#",_officelist[indexPath.row]];
[self.roadmapCollectionView addSubview:headerView.officeName];
reusableview = headerView;
}
return reusableview;
}
This is working fine(here we are creating labels and buttons in collection view header according to the figure) :-
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
CGFloat x=0,y=0;
for (int i = 0;i<[_officelist count];i++)
{
id val=[officeSize objectAtIndex:i];
CGFloat val1=[val floatValue];
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 10,val1-1,35)];
newLabel.text=[NSString stringWithFormat:#"%#",_officelist[i]];
newLabel.textAlignment = NSTextAlignmentCenter;
newLabel.backgroundColor = [UIColor greenColor];
[self.roadmapCollectionView addSubview:newLabel];
x=x+val1+1;
}
for (int i=0; i<_departmentlist.count; i++) {
dept=[_departmentlist objectAtIndex:i];
id val=[officeSize objectAtIndex:i];
CGFloat val1=[val floatValue];
float val2=val1/[dept count];
//NSLog(#"DEPT SIZE - %f",val2);
for (int j = 0; j < [dept count]; j++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(y, 50,val2-1, 25);
[button setBackgroundColor:[UIColor yellowColor]];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%#",dept[j]] forState:UIControlStateNormal];
[self.roadmapCollectionView addSubview:button];
[deptSize addObject:[NSNumber numberWithFloat:y]];
y=y+val2+1;
}
}
return reusableView;
}
Could you please try the below code (using DateFlowLayout)
bool isPortrait = UIInterfaceOrientationIsPortrait(orientation);
self.collectionView.frame = isPortrait ? CGRectMake(0, 0, 768, 180) : CGRectMake(0, 60, 246, 595);
self.collectionView.collectionViewLayout = isPortrait ? DateFlowLayout.new : UICollectionViewFlowLayout.new;
self.flowLayout = ((UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout);
self.flowLayout.scrollDirection = isPortrait ? UICollectionViewScrollDirectionHorizontal : UICollectionViewScrollDirectionVertical;
self.flowLayout.headerReferenceSize = isPortrait ? CGSizeMake(5, 30) : CGSizeMake(246, 40); //width is margin to the left of the header - must be bigger than 0 to show headers correct.
self.flowLayout.minimumInteritemSpacing = isPortrait ? 10 : 0;
self.flowLayout.minimumLineSpacing = isPortrait ? 17 : 7;
self.flowLayout.sectionInset = isPortrait ? UIEdgeInsetsMake(27, 30, 25, 0) : UIEdgeInsetsMake(0, 14, 0, 0);
//self.flowLayout.itemSize = CGSizeMake(58, 85); //You might need this
self.collectionView.alwaysBounceVertical = isPortrait ? NO : YES;
Custom UITableViewCell functions are called, but native table is presented
This is my code in the init function:
self.answersTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 45, 280, 500)];
self.answersTable.dataSource = self;
self.answersTable.delegate = self;
This is my code in the viewDidLoad function:
[self.answersTable registerClass:[AnswerTableViewCell class]
forCellReuseIdentifier:CellIdentifier];
This is my table delegate functions, which are called, and the cell is not nil in the end of it:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.question.answers count];
}
-(AnswerTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AnswerObject* answer = self.question.answers[indexPath.row];
[cell setupAnswerTableViewCell:self.question answer:answer row:indexPath.row];
return cell;
}
And here is AnswerTableViewCell:
#import "AnswerTableViewCell.h"
#implementation AnswerTableViewCell
- (id)init
{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
-(void)setupAnswerTableViewCell:(QuestionObject*)question
answer:(AnswerObject*)answer
row:(NSInteger)row{
self.question = question;
self.answer = answer;
self.row = row;
self.answerLabel.text = answer.answerText;
[self.answerToggle addTarget:self
action:#selector(flip:)
forControlEvents:UIControlEventTouchDown];
self.answerToggle.tag = [answer.answerID intValue];
if (self.row == 0) {
self.backgroundImage.image = [UIImage imageNamed:#"List_Top_Item_Not_Selected_612x113px.png"];
}
else if (self.row == ([self.question.answers count] - 1)){
self.backgroundImage.image = [UIImage imageNamed:#"List_Bottom_Item_Not_Selected_612x113px.png"];
}
else{
self.backgroundImage.image = [UIImage imageNamed:#"List_Item_Not_Selected_612x113px.png"];
}
}
So, whats wrong?
The problem is:
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
specifically: UITableViewCellStyleDefault
You're asking it to create a Cell with the default style. Either just use [[AnswerTableViewCell alloc] init]; and set the reuse identifier. Or create your own init that takes in a reuse identifier without a style param
EDIT:
After the Cell code was added I believe the issue to be that you are using the init function to add your UI elements. These are most likely being overridden by the cells UIView contentView. Its the contentView you need to customise in order to add these elements.
This article will walk you through how to do this and using the drawRect method to customise the UIView.
http://blog.giorgiocalderolla.com/2011/04/16/customizing-uitableviewcells-a-better-way/
As stupid as it may sound, the thing that fixed it for me was:
self.answersTable.backgroundColor = [UIColor clearColor];
I am trying to create something like this here.
Here is the sample code which I tried:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"RefundMeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
UIImageView *rowImageView=(UIImageView*)[cell viewWithTag:1];
UILabel *questionOptionLabel=(UILabel*)[cell viewWithTag:2];
UIImageView *collapseImageView=(UIImageView*)[cell viewWithTag:3];
UIScrollView *scrollView=(UIScrollView*)[cell viewWithTag:4];
UIImageView *sampleImageView=(UIImageView*)[cell viewWithTag:5];
UIButton *sampleButton=(UIButton*)[cell viewWithTag:6];
UILabel *instructionHeaderLabel=(UILabel*)[cell viewWithTag:7];
UILabel *instructionsLabel=(UILabel*)[cell viewWithTag:8];
UIPageControl *pgCntrl=(UIPageControl*)[cell viewWithTag:9];
[rowImageView setImage:[UIImage imageNamed:#"normalcell.png"]];
questionOptionLabel.text = #"";
collapseImageView.image = nil;
scrollView.hidden = YES;
sampleImageView.image = nil;sampleImageView.hidden = YES;
sampleButton.hidden = YES;
instructionHeaderLabel.text = #"";instructionHeaderLabel.hidden = YES;
instructionsLabel.text = #"";instructionsLabel.hidden = YES;
pgCntrl.hidden = YES;
[sampleButton addTarget:self action:#selector(userImageButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
sampleImageView.image = nil;
[collapseImageView setImage:[UIImage imageNamed:#"down.png"]];
questionOptionLabel.text=#"";
instructionHeaderLabel.text=#"Instructions";
if (indexPath.row == 0)
{
instructionHeaderLabel.hidden = NO;
sampleImageView.image = self.barcodeImage;
scrollView.hidden = NO;
scrollView.delegate = self;
pgCntrl.hidden = NO;
sampleImageView.hidden = NO;
sampleButton.hidden = NO;
pgCntrl.numberOfPages = 5;
pgCntrl.currentPage = 0;
for (int i = 0; i<5; i++)
{
[scrollView addSubview:sampleImageView];
[scrollView addSubview:sampleButton];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 5, scrollView.frame.size.height);
}
else
{
instructionHeaderLabel.hidden = NO;
sampleImageView.image = nil;
scrollView.hidden = NO;
scrollView.delegate = self;
pgCntrl.hidden = NO;
sampleImageView.hidden = NO;
sampleButton.hidden = NO;
pgCntrl.numberOfPages = 3;
pgCntrl.currentPage = 0;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3, scrollView.frame.size.height);
}
return cell;
}
My problem is, how I can add UIImageView, UILabel, etc when UIScrollView is scrolled to second page?
It should look like this
Currently UIImageView, UILabel is shown only first time. when I scroll to second page, UIImageView and UILabel scroll back and nothing is shown on second page.
Here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
tableObj.dataSource = self;
tableObj.delegate = self;
scrollView.delegate = self;
// customObj = [[customCellForText alloc]init];
imageArray = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"image1.jpg"],
[UIImage imageNamed:#"image2.jpg"],
[UIImage imageNamed:#"man-actor-Ashton-Kutcher-Wallpaper.jpg"],
[UIImage imageNamed:#"Man-Booker-Prize-judges-p-011.jpg"],
[UIImage imageNamed:#"man-curl.jpg"]
,nil];
// pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(170, 180, 100, 30)];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"cell";
customCellForText *cell = [tableObj dequeueReusableCellWithIdentifier:cellIdentifier];
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"customCellForText" owner:self options:nil];
cell = [nib objectAtIndex:0];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(130, 80, 150, 100)];
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
[scrollView setShowsHorizontalScrollIndicator:NO];
//[scrollView setShowsVerticalScrollIndicator:NO];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height);
[cell.contentView addSubview:scrollView];
for (int i = 0; i < imageArray.count; i++){
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [imageArray objectAtIndex:i];
subview.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:subview];
}
pageControl = [[UIPageControl alloc]init];
pageControl.frame = CGRectMake(170, 180, 100, 30);
pageControl.numberOfPages = imageArray.count;
pageControl.currentPage = 0;
pageControl.hidesForSinglePage = YES;
[cell.contentView addSubview:pageControl];
[pageControl addTarget:self action:#selector(changePage:) forControlEvents:UIControlEventValueChanged];
cell.nameLabel.text=#"amol";
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender{
CGFloat pageWidth = sender.frame.size.width;
int newPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = newPage;
}
I would recommend you to use a different approach because your solution is not scalable. If you want n pages you'll have to add n images & label to your scroll view which will be a mess and also not memory efficient.
Give a look at this library I think it will suit your need.
I have a subclass of UITableViewCell, and I am having problems with scrolling. All of the subviews are added to the cell in the storyboard, except for one UIView. I want to add this UIView as a subview of the cell based on a condition. The problem is that when the cells are scrolled off and onto the screen, the UIView is added to the cell a second time, or to the wrong cell. Here is my code, can you tell me what I am doing wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WavesFeedCell *cell = (WavesFeedCell *)[tableView dequeueReusableCellWithIdentifier:WavesFeedCellIdentifier forIndexPath:indexPath];
cell.cellWaveObject = [self.wavesArray objectAtIndex:indexPath.row];
//set the frames and text for the labels
cell.timeStampLabel.text = #"14m";
cell.waveTextLabel.text = cell.cellWaveObject.waveString;
cell.wavedByLabel.text = [NSString stringWithFormat:#"Waved by %#", cell.cellWaveObject.wavedByString];
//round the corners of the image view
[self setCornerRadiusForImageView:cell.profilePictureImageView];
//does the wave object have any agrees?
if (cell.cellWaveObject.numberOfAgrees > 0)
{
UIView *agreedView = [[UIView alloc] init];
UILabel *numberOfAgreesLabel = [[UILabel alloc] init];
numberOfAgreesLabel.font = [UIFont boldSystemFontOfSize:13.0f];
numberOfAgreesLabel.textColor = [UIColor whiteColor];
if (cell.cellWaveObject.numberOfAgrees > 1)
{
numberOfAgreesLabel.text = [NSString stringWithFormat:#"+%i Agree", cell.cellWaveObject.numberOfAgrees];
}
else
{
numberOfAgreesLabel.text = [NSString stringWithFormat:#"+%i Agrees", cell.cellWaveObject.numberOfAgrees];
}
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:AgreedViewImage]];
//get the width of the string
CGSize constraintSize = CGSizeMake(242.0f, 16.0f);
CGSize stringSize = [numberOfAgreesLabel.text sizeWithFont:numberOfAgreesLabel.font constrainedToSize:constraintSize];
CGFloat agreedViewWidth = stringSize.width + 10.0f;
//adjust the frame and add it to the cell
agreedView.frame = CGRectMake(310.0f - agreedViewWidth, cell.wavedByLabel.frame.origin.y, agreedViewWidth, 14.0f);
backgroundImageView.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height);
numberOfAgreesLabel.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height);
[agreedView addSubview:backgroundImageView];
[agreedView addSubview:numberOfAgreesLabel];
[cell.contentView addSubview:agreedView];
return cell;
}
else
{
return cell;
}
}
May be added subview is not removing from cell.Content View .
Try using following code within cellForRow method
UIView *agreedView=(UIView*)[cell.contentView viewWithTag:21];
//does the wave object have any agrees?
if (cell.cellWaveObject.numberOfAgrees > 0)
{
if (!agreedView) {
agreedView=[[UIView alloc] init];
agreedView.tag=21;
UILabel *numberOfAgreesLabel = [[UILabel alloc] init];
numberOfAgreesLabel.font = [UIFont boldSystemFontOfSize:13.0f];
numberOfAgreesLabel.textColor = [UIColor whiteColor];
numberOfAgreesLabel.tag=22;
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:AgreedViewImage]];
backgroundImageView.tag=23;
[agreedView addSubview:backgroundImageView];
[agreedView addSubview:numberOfAgreesLabel];
[cell.contentView addSubview:agreedView];
}
numberOfAgreesLabel=(UILabel*)[agreedView viewWithTag:22];
backgroundImageView=(UIImageView*)[agreedView viewWithTag:23];
if (cell.cellWaveObject.numberOfAgrees > 1)
{
numberOfAgreesLabel.text = [NSString stringWithFormat:#"+%i Agree", cell.cellWaveObject.numberOfAgrees];
}
else
{
numberOfAgreesLabel.text = [NSString stringWithFormat:#"+%i Agrees", cell.cellWaveObject.numberOfAgrees];
}
//get the width of the string
CGSize constraintSize = CGSizeMake(242.0f, 16.0f);
CGSize stringSize = [numberOfAgreesLabel.text sizeWithFont:numberOfAgreesLabel.font constrainedToSize:constraintSize];
CGFloat agreedViewWidth = stringSize.width + 10.0f;
//adjust the frame and add it to the cell
agreedView.frame = CGRectMake(310.0f - agreedViewWidth, cell.wavedByLabel.frame.origin.y, agreedViewWidth, 14.0f);
backgroundImageView.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height);
numberOfAgreesLabel.frame = CGRectMake(5.0f, 0.0f, agreedView.frame.size.width, agreedView.frame.size.height);
return cell;
}
else
{
if (agreedView) {
[agreedView removeFromSuperview];
}
return cell;
}
Better you set frames for your custom views in subclassed UITableView cell's class
hear is the sample code
//.h file of subclassed UITableView cell
#import <UIKit/UIKit.h>
#interface WavesFeedCell : UITableViewCell
#property(nonatomic, retain)UILabel *timeStampLabel;
#property(nonatomic, retain)UILabel *waveTextLabel;
#property(nonatomic, retain)UILabel *wavedByLabel;
#property(nonatomic, retain) id cellWaveObject; //your object from array
#end
//.m file subclassed UITableView cell
#import "WavesFeedCell.h"
#implementation WavesFeedCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectZero];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectZero];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectZero];
self.timeStampLabel = label1;
self.waveTextLabel = label2;
self.wavedByLabel = label3;
//these are added with zero rect
[self addSubview:label1];
[self addSubview:label2];
[self addSubview:label3];
//for agreed imageview
UIImageView *agreedImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
agreedImgView.tag = 200; //to access in layoutsubviews.
[self addSubview:agreedImgView];
//without ARC
[agreedImgView release];
[label1 release];
[label2 release];
[label3 release];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
//hear set frames for all labels
self.timeStampLabel.frame = CGRectMake(5, 5, self.bounds.size.width-30.0f, 40.0f);
self.waveTextLabel.frame = CGRectMake(5, 45, self.bounds.size.width-30.0f, 40.0f);
self.wavedByLabel.frame = CGRectMake(5, 95, self.bounds.size.width-30.0f, 40.0f);
//hear you check weather it is agreed or not
if(self.cellWaveObject.numberOfAgrees > 1)
{
UIImageView *agreedView = (UIImageView *)[self viewWithTag:200];
//set frme for imageview as you did
// remember dont add any views hear and set your required frames only because this method called many times
}
//in other class
//.m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WavesFeedCell *cell = [self.aTableView dequeueReusableCellWithIdentifier:#"WavesFeedCell"];
if(cell == nil)
{
cell = [[WavesFeedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"WavesFeedCell"];
}
//set cell properties
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
HomeVCTableViewCell *cell = (HomeVCTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[HomeVCTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
int imageNumber = 0;
for (int i = (indexPath.row * imagesCount); i < ((indexPath.row *imagesCount) + imagesCount); i++) {
if (i < [cellImageVOArray count]) { // If resultsArray Count is odd then we no need to create cell image
PhotoVO *photoVo = (PhotoVO *)[cellImageVOArray objectAtIndex:i];
cell.photobutton.frame=CGRectMake(((imageNumber * 5)+5)+(imageNumber * width), 2, width, height -4);
cell.photobutton.tag = i + 100;
[[cell.photobutton layer] setBorderWidth:3.0f];
[[cell.photobutton layer] setBorderColor:[UIColor whiteColor].CGColor];
[cell.photobutton addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell.photobutton setBackgroundImageWithURL:[NSURL URLWithString:photoVo.thumb_URL1] placeholderImage:[UIImage imageNamed:#"loader.png"]];
imageNumber ++;
}
}
return cell;
}
CustomCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code
NSLog(#"Content View = %f",self.contentView.frame.size.height);
photobutton=[UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:photobutton];
}
return self;
}
In the above code i got only lastbutton of the cell in every cell it was repeating please tell how to display three buttons in each cell Thanks in advance
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(0, 0, 50, 35); // set as u required.
[self.contentView addSubview:btn1];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(65, 0, 50, 35); // set as u required.
[self.contentView addSubview:btn2];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(120, 0, 50, 25); // set as u required.
[self.contentView addSubview:btn3];
}
return self;
}