UIImage is Not Displaying in UITableView - ios

I am a newbie in iOS.....I have created a table view to display as a contact view..... here i have displayed name and number using the below coding but i was not able to display images even though i used a correct code..... plz help me
names = [NSMutableArray arrayWithObjects:#"Karthick", #"Gopal", #"Suresh", #"Senthil", #"Guna",nil];
images = [NSMutableArray arrayWithObjects:#"per.png",#"per.png",#"per.png",#"per.png",#"per.png", nil];
num = [NSMutableArray arrayWithObjects:#"9568421301", #"8756324103", #"856472303", #"8796523565", #"9785858569",nil];
This is my three arrays and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
contactTableViewCell *cell = (contactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[contactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.name.text = [names objectAtIndex:indexPath.row];
cell.number.text = [num objectAtIndex:indexPath.row];
cell.conimage.image = [UIImage imageNamed:[names objectAtIndex:indexPath.row];
return cell;
}

You have to set images instead of names in cellForRowAtIndexPath method as follows
cell.conimage.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row];
replace the line in cellForRowAtIndexPathwith the above line

I see to possible issues:
UIImage +imageNamed: only works for images added to the main bundle. check, if you did that.
From the docs:
Declaration
+ (UIImage *)imageNamed:(NSString *)name
Parameters
name The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name
in the application’s main bundle.
The other issue could be that the UIImageView conimage is never created.
If this is the case, cell.conimage.image = [UIImage imageNamed:[names objectAtIndex:indexPath.row]]; would be identical to [nil setImage:aImage]; and in contrast to other languages as Java this is a valid call. Any message sent to the nil object will result in nil — and not an exception.
To be able to see, if this indeed is the case, you will have to explain (preferable with code) how your custom cell class looks like.

Related

Custom cells in UITableView become blank after scrolling up or down

I was tasked to solve this bug in the app (It's for comercial use so I can't link the project). I'm also completely new to Objective-C or IOS development and I have no idea why the following happens. Reminder, I'm using custom cells.
When the tableview loads, everything looks fine.
Click here to see the example
But when I scroll up and back to the same postion the cell becomes blank, as if it had no data. Blank cell
If I repeat the same process everything looks fine again.
Here's the code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"cell2";
PedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
cell = [(PedTableCell*)[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
if (tableView==_table3)
{
NSString *docsDirr;
NSString *documentsDirectoryForSaveImages;
docsDirr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
documentsDirectoryForSaveImages = [docsDirr stringByAppendingPathComponent:#"/CarpetaImatges"];
NSString *match = #".jpg";
NSString *preCodigo;
NSScanner *scanner = [NSScanner scannerWithString:[pedarray objectAtIndex:indexPath.row]];
[scanner scanUpToString:match intoString:&preCodigo];
NSString *nomimg=[NSString stringWithFormat:#"%#.jpg", preCodigo];
UIImage *img = [UIImage imageWithContentsOfFile:[documentsDirectoryForSaveImages stringByAppendingPathComponent:nomimg]];
cell.img.image = img;
cell.layer.borderWidth=1.0f;
cell.img.layer.borderWidth=1.0f;
cell.cart.text = preCodigo;
cell.descart.text = [nomarray objectAtIndex:indexPath.row];
cell.cant.text = [cantarray objectAtIndex:indexPath.row];
cell.precart.text = [precioarray objectAtIndex:indexPath.row];
cell.pvpcart.text = [pvparray objectAtIndex:indexPath.row];
[cell layoutIfNeeded];
}
return cell;
}
I've debugged the method and everytime it returns a valid cell. When I click the empty cell it still works as expected. Essentially, the data is there, it's just not rendered for some reason.
I've checked other posts with the same/similar issue but nothing seems to really match. PrepareForReuse is a no go since there's no UI to be changed, just content. I'm getting the values of the arrays with indexPath.row to make sure I'm fetching the correct value. I've even tried to set the heightForRowAtIndexPath to the same height the cell should have (all cells are of the same size and never change) or let AutoLayout handle it but to no avail.
Code of numberOfRowsInSection (Returns the amount of orders that exists in database currently):
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(tableView == _table3)
{
[self cargacarro];
return pedarray.count;
}
return pedarray.count;
}
Any ideas?
Register your cell nib in storyboard or in code before tableview is loaded, and after replace dequeueReusableCell... with this one
PedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
cell.img...
cell.cart...
remove this part, is no longer needed
if (cell == nil)
{
cell = [(PedTableCell*)[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}

Display custom TableView cells using plist?

I want to display a small image and date inside of a Table View cell, which the user can click on to display another view with more information. All of this data is stored in a plist (image, date, and other numbers).
I can't seem to:
Get the images to display inside of the cells - I added all of my images into the Supporting Files folder and tried to enter the filenames into the plist which it did not accept.
Add the functionality for the user to click on a cell and view more information from the plist data.
First you construct your "database" using plist which contains an array of dictionary. This should be something like this:
Create and NSArray to hold this plist:
#property (strong, nonatomic) NSArray *imagesList;
And do the loading in viewDidLoad (suppose that the plist named "ImagesList"):
NSString *path = [[NSBundle mainBundle] pathForResource:#"ImagesList" ofType:#"plist"];
self.imagesList = [[NSArray alloc] initWithContentsOfFile:path];
The rest is some simple UITableViewDatasource:
#pragma mark - UITableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.imagesList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:#"myCell"];
}
NSDictionary *eachImage = [self.imagesList objectAtIndex:indexPath.row];
cell.textLabel.text = [eachImage objectForKey:#"date"];
cell.detailTextLabel.text = [eachImage objectForKey:#"additionalDetail"];
cell.imageView.image = [UIImage imageNamed:[eachImage objectForKey:#"imageName"]];
return cell;
}
Don't forget to put your images into app bundle and rename it properly.

Issue with running through NSArray - Obj C

I have an NSDictionary objectreturned from my server. I am using it to populate information on a UITableViewController. My issue is that when I am processing each object returned, it seems to duplicate, lets say I get 2 objects returned. It will make 2 table cells but with only the first object's data. So lets say the title section from both objects is "yes" and "no". It would populate the list with the first object title so it would show, yes and yes.
Here is what happens:
Controller is loaded:
- (void)viewDidLoad
{
[super viewDidLoad];
// MAKE REQuEST TO SERVER
[self makeRequests];
}
runs the request from server which stores information as follow:
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:returnData //1
options:kNilOptions
error:&error];
self.googlePlacesArrayFromAFNetworking = [json objectForKey:#"requested_data"];
[self.tableView reloadData];
than the cellForRowAtIndexPath runs:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"timelineCell";
FBGTimelineCell *cell = (FBGTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[FBGTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.usernameLabel.text = [_googlePlacesArrayFromAFNetworking[indexPath.row] objectForKey:#"name"]; <!-- here is where the title example I was talking about happens
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell initTimelineCell];
}
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg", indexPath.row]];
cell.photoView.image = img;
return cell;
}
Here is my googlePlacesArr...
#property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;
Here is my numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.googlePlacesArrayFromAFNetworking count];
}
Suggestions thoughts? Let me know if you need anything else.
UPDATE:
Also I think its important to note I am using this:
https://github.com/Seitk/FB-Gallery
As a base for populating my returned data.
This usually happens when the cell gets recycled if your tableView:cellForRowAtIndexPath: method fails to set some of the members of recycled objects.
That is precisely what happens in your code: when the cell is recycled, you never set its usernameLabel.text, so the recycled value may be shown multiple times.
You should move the code that sets up cell's properties outside of the if statement to fix this problem:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"timelineCell";
FBGTimelineCell *cell = (FBGTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[FBGTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell initTimelineCell];
}
// This line is moved from the "if" statement
cell.usernameLabel.text = [_googlePlacesArrayFromAFNetworking[indexPath.row] objectForKey:#"name"];
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg", indexPath.row]];
cell.photoView.image = img;
return cell;
}
Try this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* /////// Custom code ////// */
cell.usernameLabel.text = [_googlePlacesArrayFromAFNetworking[indexPath.row] objectForKey:#"your_key_name"];
}

UIImage return nil only in a UIImageView

I'm new in iOS programming, and I have a strange problem...
I created a tableview, add cells with "subtitle" style. In my code, I can set the image, all works fine. the code (g, gf and picturesArray are strings array) :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellIdentifier = #"fCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSString *feed = [self.g objectAtIndex:indexPath.row];
NSString *feed1 = [self.gf objectAtIndex:indexPath.row];
NSString *feed2 = [self.picturesArray objectAtIndex:indexPath.row];
[cell.textLabel setText:feed];
[cell.detailTextLabel setText:feed1];
cell.imageView.image = [UIImage imageNamed:feed2];
return cell;
}
Next, I want to go to a new view when I click on a cell. Here the code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
GUDdfViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"GUdfView"];
detail.titleString = [self.gf objectAtIndex:indexPath.row];
detail.descriptionString = [self.gfs objectAtIndex:indexPath.row];
detail.contentString = [self.gfc objectAtIndex:indexPath.row];
detail.imageNameString = [self.picturesArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detail animated:YES];
}
OK. All works; in my new view, title and subtitle are present with good content.
BUT ! the image is not displayed... Here the line where I set the image in "GUDdfViewController" :
[self.pictureLogoOutlet setImage:[UIImage imageNamed:self.imageNameString]];
When I use NSLog, it says me that my object (pictureLogoOutlet.image) is null. I also tried this :
[self.pictureLogoOutlet setImage:[UIImage imageNamed:#"baguette.png"]];
but the problem is the same...
Do you know why I have this problem ? I really don't understand.
pictureLogoOutlet in nil because your GUDdfViewController has not yet initialized its IBOutlets.
You have to create property of UIImage to your viewocntroller and using that property load imageView in your GUDdfViewController's viewDidLoad.

iOS: UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

I've checked a few other questions but still can't figure out why this is happening. All I know is after using the description code from this question the cell's value is null. I've set the delegate and datasource of the table correctly and I can't see where this is going wrong.
If I set the return value of the following method to 0, no error occurs because the cellForRowAtIndexPath method doesn't really take place. So if I set it as 1 (as it should be) it'll then throw the error. I've synthesized the NSArray and checked that its populated (although that shouldn't matter I guess) and basically what is meant to happen is I press a button that searches and then places the results in the table. So before this the tableview is empty.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Here's the cellForRowAtIndexPath method itself. I've tried just using the basic template method too, which still throws the same error.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSDictionary *aResult = [results objectAtIndex:[indexPath row]];
NSLog(#"RESULTS IN TABLE %i", [results count ]);
id key = [results objectAtIndex:[indexPath row]];
resultTitle.text=#"Hello";
NSURL *url = [NSURL URLWithString:[aResult objectForKey:#"url"]];
NSData *data = [NSData dataWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:data];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[theTableView reloadData];
return cell;
}
I've no idea what is wrong. I've used tableviews before and have compared this project to others to see if I've missed something, but I can't see any real differences.
you are not initisialing the cell if nil is returned from the dequeue method
change
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
to
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
You do not have a method to return the number of rows in section 0 - this is a required data source method (in this case).

Resources