Loading images in each section - uitableview

I am currently using Parse as my back end and have a few photos stored on their servers. When I go to query them into my table view, every section comes up with the same image over and over again instead of having different images for each user. I will post my code for the cellForRowAtIndexPath I just don't understand what is going on. And each section has it's own header to display the user's name who posted the image.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger sections = self.userPhotos.count;
return sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cellIdentifier";
HomeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[HomeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Setting the image in the cell.
PFObject *carPhoto = [self.userPhotos objectAtIndex:indexPath.row];
PFFile *imageFile = [carPhoto objectForKey:#"imageFile"];
NSURL *imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
cell.carImage.contentMode = UIViewContentModeScaleAspectFit;
cell.carImage.image = [UIImage imageWithData:imageData];
return cell;
}
Thank you for your help in advance!

Related

XML Parsing getting id

how to get all id's from this url and display in uitableview "http://www.thomas-bayer.com/sqlrest/CUSTOMER/".
- (void)viewDidLoad
{
NSURL *URL = [[NSURL alloc] initWithString:#"http://www.thomas-bayer.com/sqlrest/CUSTOMER/"];
NSString *xmlString = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:NULL];
xmlDoc = [NSDictionary dictionaryWithXMLString:xmlString];
NSLog(#"dictionary: %#", xmlDoc);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [xmlDoc count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [[[xmlDoc objectForKey:#"CUSTOMER"]objectAtIndex:indexPath.row]objectForKey:#"__text"];
return cell;
}
I am getting only 0,1,2 values displayed in uitableview and ignoring the remaining id's ?
Use this Library to convert XMLData into NSDictionary object.
https://github.com/nicklockwood/XMLDictionary then simply fetch your required data from NSDictionary object and update it into your TableView.

How to add images to UITableView from phone gallery

I created in storyboard UITableView with prototype cell, TableViewController and UITableViewCell. Now I would like to choose all the pictures from phone gallery and save one or more to the database. But at this moment I need to implement adding pictures to tableview. I'm beginner in iOS. Can anyone tell me how can do that? give me a link or code? Thank you for advance
You can use ALAssetsLibrary in AssetsLibrary framework to fetch all images from phone gallery. Save each images in to an Array.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
NSURL *url= (NSURL*) [[result defaultRepresentation] url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
UIImage *img = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
[imgArray addbject:img];
}
failureBlock:^(NSError *error){ NSLog(#"operation failed"); } ];
}
}
};
Then you can use this array for saving to DB or to show in tableview.
For Using in TableView, you need to add UIImageView in your prototype cell, then set the images from the array to the cell accordingly. Your table view delegate methods will be like below.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [imgArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellId = #"ImageCell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];
}
UIImage *img = [imgArray objectAtIndex:indexPath.row];
cell.imgView.image = img;
return cell;
}
If you want to pick the image from gallery and show it to tableview, you can use Custom ImageView in tableView or CustomCell imageView
First you need to pick the image from gallery and save it to array
Before that allocate and initialize the array in viewDidLoad method
ViewController.m
#import "ViewController.h"
#interface ViewController ()
{
NSMutableArray *arrayImage;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arrayImage = [[NSMutableArray alloc]init];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image=[info objectForKey:#"UIImagePickerControllerOriginalImage"];
imageView.image=image;
[arrayImage addObject:image];
picker.delegate =self;
[picker dismissViewControllerAnimated:YES completion:nil];
}
Then in tableView
If you CustomCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell==nil)
{
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = nib[0];
}
cell.galleryImageView.image = [arrayImage objectAtIndex:indexPath.row];
return cell;
}
If you use default table view cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
cell.imageView.image = [arrayImage objectAtIndex:indexPath.row];
return cell;
}

Same UITableViewCell when using JSON

When my UITableview loads I am getting the same cell over and over. I'm assuming I need to add something about indexpath.row but this is my first time using JSON so I'm not sure exactly how to do it. Attached is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
}
NSArray *subArray = [subDict objectForKey:#"items"];
if ([subArray count]) {
for (NSDictionary *dictItem in subArray) {
cell.titleLabel.text = [dictItem objectForKey:#"title"];
NSNumber *totalSeconds = [dictItem objectForKey:#"duration"];
int *minutes = totalSeconds.intValue/60;
int *seconds = totalSeconds.intValue%60;
NSString *finalDuration =[NSString stringWithFormat:#"%im %is", minutes,seconds];
cell.durationLabel.text = finalDuration;
NSString *separatorString = #"T";
NSString *firstUploadString = [dictItem objectForKey:#"uploaded"];
NSString *uploadString = [firstUploadString componentsSeparatedByString:separatorString].firstObject;
cell.dateUploadLabel.text = uploadString;
NSString *viewCount = [NSString stringWithFormat:#"%#",[dictItem objectForKey:#"viewCount"]];
cell.viewsLabel.text = viewCount;
NSDictionary *playBackDict = [dictItem objectForKey:#"player"];
NSString *playBackURL = [playBackDict objectForKey:#"mobile"];
NSLog(playBackURL);
NSDictionary *thumbnailDict = [dictItem objectForKey:#"thumbnail"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *thumbnailURL = [thumbnailDict objectForKey:#"sqDefault"];
NSURL *url = [NSURL URLWithString:thumbnailURL];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *thumbNailImage = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
// Here in main thread set image to your cell imageView.
// e.g.
[cell.videoThumbnail setImage:thumbNailImage];
});
});
};
}
return cell;
}
I'm just having trouble iterating through these so any help would be awesome. Thanks!!
You are iterating over the subArray variable and configuring your cell for each item in the array. So all your cells always are going to have the same data that it correspondes with the last item of your array.
You need to use the indexPath parameters that receive in the (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to get the correct object for each cell, similar to this snippet:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell... if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
NSArray *subArray = [subDict objectForKey:#"items"];
// You should check here the array size before get the item
NSDictionary *dictItem = [subArray objectAtIndex:indexPath.row];
NSNumber *totalSeconds = [dictItem objectForKey:#"duration"];
int *minutes = totalSeconds.intValue/60;
int *seconds = totalSeconds.intValue%60;
NSString *finalDuration =[NSString stringWithFormat:#"%im %is", minutes,seconds];
cell.durationLabel.text = finalDuration;
// ToDo: all your configuration here using dictItem
return cell;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called for the tableView one time for each row depending on the value returned by (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView and (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

How to Set a array data First value in Tableview First Section and remaining array data value in Second Section of tableview in iOS?

i am newbie in iOS and i know this Question is asked for many times but i am not getting solution.i made an application with tableview here is my tableview code Containing two section and i want to Fill this section with JSON Data.i made Two CustomCell. it Display well but i am not able to fill my JSON Parsing Data.
my code is.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *CellIdentifierOne=#"CellOne";
CustumCell *cellOne =[tableView dequeueReusableCellWithIdentifier:CellIdentifierOne];
if (cellOne == nil)
{
NSArray *nibOne=[[NSBundle mainBundle]loadNibNamed:#"CustumCell" owner:self options:nil];
cellOne=[nibOne objectAtIndex:0];
cellOne.userInteractionEnabled=NO;
}
{
[cellOne.spinner startAnimating];
NSDictionary *dict = [self.imageArray objectAtIndex:indexPath.section];
NSString *img2=[dict valueForKey:#"front_image"];
[cellOne.storeImage sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:#"Setting.png"] options:SDWebImageHighPriority completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
{
[cellOne.spinner stopAnimating];
cellOne.spinner.hidesWhenStopped=YES;
}];
}
return cellOne;
}
else
{
static NSString *CellIdentifierTwo=#"CellTwo";
TableCell *cellTwo=[tableView dequeueReusableCellWithIdentifier:CellIdentifierTwo];
if (cellTwo == nil)
{
NSArray *nibTwo=[[NSBundle mainBundle]loadNibNamed:#"TableCell" owner:self options:nil];
cellTwo=[nibTwo objectAtIndex:0];
cellTwo.userInteractionEnabled=NO;
}
return cellTwo;
}
return nil;
}
And Here Tableview section is Two and also code for numberofRow is
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return 1;
NSLog(#"Images %#",self.imageArray);
}
else
{
return self.imageArray.count - 1;
}
}
when there is no any Data then it Shows as i want, but i am not able to fill my Data Please provide me any Solution.
#AshishGabani
look at the below code, what i understand for your requirement
I have taken an array, contains values like this
NSMutableArray *imageArray = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",nil];
Next TableView Methods:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) return 1;
return imageArray.count-1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"Simple";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.section==0) {
cell.textLabel.text = [imageArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [imageArray objectAtIndex:indexPath.row+1];
}
return cell;
}
Just Comment you existing code and Copy this code and Paste your Xcode ViewController and Run the simulator, I think i reached your requirement .

How To Change UITableView Cell Height Equally & Show detailedTextLabel?

Hi I have this code here.
- (void)viewDidLoad
{
[super viewDidLoad];
jsonURL = [NSURL URLWithString:#"http://oo.mu/json2.php"];
jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL usedEncoding:nil error:nil];
self.jsonArray = [jsonData JSONValue];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [jsonArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row] * 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[self.jsonArray objectAtIndex:indexPath.row] objectForKey:#"Name"];
return cell;
}
What I want is:
To change the height of the UITableView cell so I can fit more
things under the textLabel. (Currently, when I use the previous
code to increase the height of the UITableView cell, each cell
goes from big to small in different sizes).
To show under the textLabel.text a detailedTextLabel of
something else under it.
How would I do that?
I tried:
cell.textLabel.text = [[self.jsonArray objectAtIndex:indexPath.row] objectForKey:#"Name"];
cell.detailTextLabel.text = [[self.jsonArray objectAtIndex:indexPath.row] objectForKey:#"Street"];
cell.detailTextLabel.text = [[self.jsonArray objectAtIndex:indexPath.row] objectForKey:#"City"];
But it doesn't show up.
You can create new UItableview cell with the dimensions as per your requirements. Later
-(Uitableview) cellForIndexPath {
//add the customized Uitableviewcell.
}
In this way you can create the tableview cell as per your requirements. I hope it will help.

Resources