How to use image from url in UICollectionView ? - iphone

My code is :
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(30, 20, 260, 270) collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[collectionView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:collectionView];
string1=[NSString stringWithFormat:url];
NSURL *jsonUrl = [NSURL URLWithString:string1];
NSData *data = [NSData dataWithContentsOfURL:jsonUrl];
UIImage *image1 =[[UIImage imageWithData:data]autorelease];
set=[NSArray arrayWithObject:image1];
tmp= [[NSMutableArray alloc] init];
for (int i=1; i<[set count]; i++)
{
[tmp addObject:[set objectAtIndex:i+1]];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [tmp count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[tmp objectAtIndex:indexpath.row]]] ;
cell.backgroundColor=[UIColor clearColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(80, 80);
}
and I feel that this action does not work or when I hurt things
cell.backgroundview =[UIImageView alloc]initwithimage:[UIImage imageNamed:[tmp objectAtIndex:indexPath.row]]];

You do something wrong in your viewDidLoad
The only thing you need is to set a color to your UICollectionView so change your code like this for example :
[collectionView setBackgroundColor:[UIColor redColor]];
Assuming your array tmp is an array of image name (like "myImage.png") and and reload your collection view before the end of your viewDidLoad ;
[collectionView reloadData];

I have the solution for you try like that:
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"nameImage.jpg"]];

Related

Load more at the top of uitableview

I need a load more cell at the top of my table populated with custom cell, the first time that I populate the table is all ok, no overlap and no problem, if I try to load more cell this cell appear but with the wrong height and if I try to scroll the cell change position randomly!
This is my code, where the mistake for you?
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arrayMessaggi count] + 1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row == 0) {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *labelLoad = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[labelLoad setBackgroundColor:[UIColor clearColor]];
[labelLoad setTextAlignment:NSTextAlignmentCenter];
[labelLoad setText:[NSString stringWithFormat:#"%d - Load more...",indexPath.row]];
[labelLoad setText:#"Load more..."];
[labelLoad setTag:3];
[cell.contentView addSubview:labelLoad];
}
UILabel *labelLoad = (UILabel *)[cell.contentView viewWithTag:3];
[labelLoad setText:[NSString stringWithFormat:#"%d - Load more...",indexPath.row]];
}
else {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
PFUser *user = [obj objectForKey:#"daUtente"];
float height = [self getStringHeight:[obj objectForKey:#"TestoMessaggio"] andFont:[UIFont systemFontOfSize:13]];
UILabel *labelUser = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
[labelUser setBackgroundColor:[UIColor clearColor]];
[labelUser setText:[user objectForKey:#"username"]];
[labelUser setFont:[UIFont systemFontOfSize:13]];
[labelUser setTag:1];
[cell.contentView addSubview:labelUser];
}
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
PFUser *user = [obj objectForKey:#"daUtente"];
UILabel *labelUser = (UILabel *)[cell.contentView viewWithTag:1];
[labelUser setText:[NSString stringWithFormat:#"%d - %#",indexPath.row,[user objectForKey:#"username"]]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height = 44;
if (indexPath.row > 0) {
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
NSString *text = [obj objectForKey:#"TestoMessaggio"];
height = 30 + [self getStringHeight:text andFont:[UIFont systemFontOfSize:13]] + 10;
}
return height;
}
- (void) setPullDownToRefresh {
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(aggiornaTabella:) forControlEvents:UIControlEventValueChanged];
[self.messagesTableView addSubview:refreshControl];
}
- (void) aggiornaTabella:(UIRefreshControl *)myRefreshControl {
self.arrayMessaggi = [NSMutableArray arrayWithArray:objects];
self.messagesTableView = [[UITableView alloc] init];
[self.messagesTableView reload data];
}

Scrolling horizontal and vertical both direction

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)];
}

UICollectionView: header view programmatically [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is the way i am working on header.
// viewDidLoad
[self.collectionView registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:#"Cell"];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"header"];
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 390, 300) collectionViewLayout:layout];
layout.collectionView.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
[self.collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:#"Cell"];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
layout.minimumInteritemSpacing = 15;
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.collectionView.collectionViewLayout = layout;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.showsVerticalScrollIndicator = NO;
[self.collectionView setBounces:YES];
self.collectionView.allowsMultipleSelection = YES;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"HorizontalPickerCell"];
[self.view addSubview:self.collectionView];
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return CGSizeMake(0, kHeaderHeight);
}
return CGSizeMake(0, kHeaderHeight + kInterSectionMargin);
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
NSLog(#"levels count:%d", [arrLevels count]);
return [arrLevels count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(#"vals count:%d", [arrSeats count]);
for(int i=0; i<[arrLevels count]; i++)
{
if(section == i)
{
int c;
NSString *cnt = [NSString stringWithFormat:#"%#",[arrTot objectAtIndex:i]];
c = [cnt intValue];
return c;
}
}
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
//cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"blue_s.png"]];
if (cell.selected) {
[UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_s.png"]]; // highlight selection
}
else
{
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yellow_seat.png"]]; // Default color
}
return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 12, 10, 10);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize retval = CGSizeMake(22, 20);
return retval;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:nil forIndexPath:indexPath];
self.lblHeader = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 100, 30)];
self.lblHeader.backgroundColor = [UIColor whiteColor];
self.lblHeader.textColor = [UIColor redColor];
self.lblHeader.text = #"Level 1";
[self.collectionView addSubview:self.lblHeader];
}
return reusableview;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
printf("Selected View index=%d",indexPath.row);
itemPaths = [self.collectionView indexPathsForSelectedItems];
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_s.png"]];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yellow_seat.png"]];
}
The error you're seeing is pretty clear. You're trying to dequeue a reusable view but the collection view doesn't know anything about the reuse identifier you're passing.
You need to call registerClass:forSupplementaryViewOfKind:withReuseIdentifier: before dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: is called. I typically call one of the register methods in viewDidLoad so all the setup is done and I can just call the dequeue method. If you're loading your custom header and footer views from nib you can use the registerNib:forSupplementaryViewOfKind:withReuseIdentifier: method.
Edit
I see you updated the code. You need to move these lines
UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 390, 300) collectionViewLayout:layout];
above the lines where you're calling the register class methods. You cannot send messages to an object before you allocate and initialize it.
If you are not displaying anything on the header then how could you expect header view to show you something on header part of UICollectionView.Try following code.This is working code
- (void)viewDidLoad
{
arry=[[NSArray alloc] initWithObjects:#"One",#"Two",#"Three",#"Four", nil];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView"];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arry count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 20;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
if (cell==nil) {
cell=[[UICollectionViewCell alloc] init];
}
UILabel *label = (UILabel*)[cell.contentView viewWithTag:21];
if (label==nil) {
label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
label.tag = 21;
[cell.contentView addSubview:label];
}
label.text = [arry objectAtIndex:indexPath.row];
NSLog(#"cell is %#",NSStringFromCGRect(cell.frame));
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(60, 60);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets insets=UIEdgeInsetsMake(10, 10, 10, 10);
return insets;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
if (reusableview==nil) {
reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
label.text=[NSString stringWithFormat:#"Recipe Group #%i", indexPath.section + 1];
[reusableview addSubview:label];
return reusableview;
}
return nil;
}
why are you adding lblHeader to self.collectionView add to headerView.And return headerView.Replace your code with it
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:nil forIndexPath:indexPath];
self.lblHeader = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 100, 30)];
self.lblHeader.backgroundColor = [UIColor whiteColor];
self.lblHeader.textColor = [UIColor redColor];
self.lblHeader.text = #"Level 1";
// [self.collectionView addSubview:self.lblHeader];
[headerView addSubview:self.lblHeader];
return headerView;
}
Typically you put this register line in the viewDidLoad or some sort of initialize so that it is not called over and over again. But mainly you have some confusion. You register a class for the header but you dequeue a class on the footer. You should register a class for both and dequeue the right one for each.
Notice your register uses UICollectionElementKindSectionHeader:
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"header"];
Notice your dequeue uses UICollectionElementKindSectionFooter :
UICollectionReusableView *supplementaryView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:#"header" forIndexPath:indexPath];
hope that helps.
try this :
//ON top
static NSString *headerViewIdentifier = #"Test Header View";
static NSString *footerViewIdentifier = #"Test Footer View";
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
identifier = headerViewIdentifier;
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
identifier = footerViewIdentifier;
}
// TODO Setup view
UICollectionReusableView *supplementaryView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:identifier forIndexPath:indexPath];
return supplementaryView;
}

how to remove text from cell when click on the text of cell

hi i have problem when i am displaying the records of table by clicking on load more cell . problem is this when i click on load more cell then table must load two more records and then load more text must move forward for example first time 2 records load on table at the 3rd cell is the load more cell when i click on this cell then two more records must load into table and this load more text move to 5th cell . but in my case the problem is when i click on load more cell at 3rd position it also move to fifth position but on 3rd cell record not load and also load more text appear on 3rd cell. kindly tell me how i can handle this problem this is the code
#import "RootViewController.h"
#import "XMLAppDelegate.h"
#import "song.h"
#import "BookDetailViewController.h"
#implementation RootViewController
#synthesize mylabel,mylabel1,mylabel2,spinner,cell;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([myarray count]>pageSize)
{
return pageSize+1;
}
return myarray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row ]]];
UIImage *image=[UIImage imageWithData:data];
if(indexPath.row<pageSize)
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 60, 60)];
imageView.image=image;
[cell addSubview:imageView];
CGRect CellFrame1 = CGRectMake(65, 10, 220, 45);
mylabel1 = [[UILabel alloc] initWithFrame:CellFrame1];
mylabel1.text = asong.title;
mylabel1.font = [UIFont boldSystemFontOfSize:12];
mylabel1.textColor = [UIColor blackColor];
[cell.contentView addSubview:mylabel1];
CGRect CellFrame2 = CGRectMake(65, 47, 150, 25);
mylabel2 = [[UILabel alloc] initWithFrame:CellFrame2];
mylabel2.text = asong.artist;
mylabel2.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:mylabel2];
CGRect CellFrame = CGRectMake(255, 3, 60, 7);
mylabel = [[UILabel alloc] initWithFrame:CellFrame];
mylabel.text = asong.duration;
mylabel.textColor = [UIColor blueColor];
mylabel.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:mylabel];
}
else
{
cell.textLabel.text = #"load more";
spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner performSelector:#selector(stopAnimating) withObject:nil afterDelay:1]; [spinner release];
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
if (indexPath.row==pageSize)
{
pageSize=pageSize+2;
[cell reloadInputViews];
[tableView reloadData];
}
/* else if(bdvController == nil)
bdvController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:[NSBundle mainBundle]];
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
bdvController.aartist = asong;
[self.navigationController pushViewController:bdvController animated:YES];*/
}
- (void)viewDidLoad {
[super viewDidLoad];
pageSize =2;
// Uncomment the following line to add the Edit button to the navigation bar.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
myarray = [[NSMutableArray alloc] initWithObjects:#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png",#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png",#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png", nil];
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title = #"Songs";
}
/*
// Override to support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[bdvController release];
[appDelegate release];
[super dealloc];
}
#end
Your problem is that you're only configuring a cell if it's nil. In your example, you will want to replace cell 3's content with some song even when it's not nil.
When deque returns nil, create a new cell and add subviews but always update the content based on the path.
Try below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row<pageSize) {
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 60, 60)];
imageView.tag = 1;
[cell addSubview:imageView];
CGRect CellFrame1 = CGRectMake(65, 10, 220, 45);
mylabel1 = [[UILabel alloc] initWithFrame:CellFrame1];
mylabel1.tag = 2;
mylabel1.font = [UIFont boldSystemFontOfSize:12];
mylabel1.textColor = [UIColor blackColor];
[cell.contentView addSubview:mylabel1];
CGRect CellFrame2 = CGRectMake(65, 47, 150, 25);
mylabel2 = [[UILabel alloc] initWithFrame:CellFrame2];
mylabel2.tag = 2;
mylabel2.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:mylabel2];
CGRect CellFrame = CGRectMake(255, 3, 60, 7);
mylabel = [[UILabel alloc] initWithFrame:CellFrame];
mylabel.tag = 2;
mylabel.textColor = [UIColor blueColor];
mylabel.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:mylabel];
}
UIImageView *imageView = [cell viewWithTag:1];
UILabel *mylabel1 = [cell viewWithTag:1];
UILabel *mylabel2 = [cell viewWithTag:1];
UILabel *mylabel = [cell viewWithTag:1];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row ]]];
dispatch_sync(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:data];
});
});
mylabel1.text = asong.title;
mylabel2.text = asong.artist;
mylabel.text = asong.duration;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
static NSString *CellIdentifier = #"cellloadmore";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = #"load more";
spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner performSelector:#selector(stopAnimating) withObject:nil afterDelay:1]; [spinner release];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

setIndentationLevel is not working for cell.ContentView subviews

Why setIndentationLevel is not getting with cells subviews?
I have a UITableView. My cellForRowAtIndexPath is like below. The arrow which i added is not shifting with the textlabel. why?
How can I do this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"small_arrow.png"]];
img.frame = CGRectMake(5, 10, 7, 11);
[cell.contentView addSubview:img];
cell.textLabel.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"name"];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:18];
[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
Yeah !!! Its workin now.
I created a custom cell and its m file is like
#import "customecell.h"
#implementation customecell
#synthesize textLbl,img;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
textLbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 320, 20)];
textLbl.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:textLbl];
img = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 7, 11)];
img.image = [UIImage imageNamed:#"small_arrow.png"];
[self.contentView addSubview:img];
}
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];
float indentPoints = self.indentationLevel * self.indentationWidth;
self.contentView.frame = CGRectMake(
indentPoints,
self.contentView.frame.origin.y,
self.contentView.frame.size.width - indentPoints,
self.contentView.frame.size.height
);
}
#end
and the cellForRowAtIndexPath is changed to
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
customecell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[customecell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLbl.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"name"];
cell.textLbl.font = [UIFont fontWithName:#"Helvetica" size:18];
[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
cell.indentationWidth = 25;
float indentPoints = cell.indentationLevel * cell.indentationWidth;
cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height);
return cell;
}