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.
Related
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.
I want to update button images in my table view. When my table view initially loads, the button images are correct. But when I scroll down the new cells don't update to my new images; they simply reuse a random old image from the initial set. I have tried setImage and setBackgroundImage. Here is my cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
SSKGrooveCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"grooveCell" forIndexPath:indexPath];
SSKSongCellInfo *entry = [_objects objectAtIndex:indexPath.row];
NSArray *icons = [plistHelper iconsForGroove:entry.metadata.title];
NSString *icon = [icons objectAtIndex:0];
NSString *iconImgString = [NSString stringWithFormat:#"element_%#_grooves", icon];
NSString *iconImgWithExt = [NSString stringWithFormat:#"%#.png", iconImgString];
[cell.inst1Button setBackgroundImage:[UIImage imageNamed:iconImgWithExt] forState:UIControlStateNormal];
cell.titleLabel.text = entry.metadata.title;
return cell;
}
return nil;
}
For a quick test, add this line in your cellForRowAtIndexPath
cell.imageView.image = [UIImage imageNamed:iconImgWithExt];
Do you see correct images?
If no, check your images and image names
If yes, check your inst1Button IBOutlet connection.
I just came across this issue on an app. Turns out there's a bug in iOS 7.1 regarding a UIButton in a UITableView. You need to call setNeedsLayout on the button after you've set the image. E.g.:
[cell.inst1Button setNeedsLayout];
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"];
}
I am facing a weird issue and I am sure I am doing something wrong. In my UITableViewController class I have a function as follows:
-(void)EERefreshView
{
NSLog(#"App delegate requested the view be refreshed");
[self loadEventsFromDB];
for(int a=0;a<eventsList.count;a++)
{
DB_EEEvent *event = (DB_EEEvent*)eventsList[a];
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
//NSLog(#"CellID %#", cell.reuseIdentifier);
//get references to the UI components
UILabel *lblTitle = (UILabel*)[cell.contentView viewWithTag:5];
UILabel *lblDate = (UILabel*) [cell.contentView viewWithTag:6];
UIImageView *imgEvent = (UIImageView*)[cell.contentView viewWithTag:7];
//set values
lblTitle.text = event.name;
lblDate.text = [NSString stringWithFormat:#"%# - %#",
event.startdate,event.enddate];
AppDelegate *del = [AppDelegate sharedAppDelegate]; //need this to get the BaseURL
// Here we use the new provided setImageWithURL: method to load the web image
NSString * imageURL =[[NSString alloc] initWithFormat:#"http://%#%#",del.api.BaseURL,event.logo];
NSString* webStringURL = [imageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:webStringURL];
[imgEvent setImageWithURL:url placeholderImage:[UIImage imageNamed:#"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
//do somethign when image call completes
}];
[eventsListCells addObject:cell];
if(a == selectedEvent)
{
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[tblEventsList reloadData];
});
}
This function loads an array with UITableViewCell objects which is then used later on by
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell data
NSLog(#"IndexPathRow: %d", indexPath.row);
UITableViewCell *cell = (UITableViewCell*)eventsListCells[indexPath.row];
return cell;
}
However when the cell is loaded lblTitle and lblDate have no text displaying in them. But when I tap on a cell it then displays the text in the labels. So this means that the data is there but its not being refresh when initially created. I suspect this might have something to do with threads but I am not an expert on the subject and would appreciate any help that I can get.
I see what is bothering you and why you created this method, but you don't need it. The only reason why you need back thread is your image loading so that should be thrown in bkg. thread and the rest of the code should be in -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.
U really don't need to pre create cells, and U cant cause they are reusable. For example if you have 50 elements in eventsList and room for only 5 cell on your screen you cant create 50 cells cause the code will create only 5+1 and reuse them as they appear on the screen, so every element of your eventsList will use one of those 5 cells and on the other side every cell will be a host for 10 different elements from your eventsList array. So as you can see your code doesn't make much sense...
The second function you mentioned in your question is the function that is called to populate the table with data. When you call [tblEventsList reloadData]; your app just calls the (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method. So this method needs to be outputting data back to the table. A very simple example is below. Also, make sure your table delegate and data source are set.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.textLabel.text = [self.your_data_source objectAtIndex:[indexPath row]];
return cell;
}
I'm trying to pass values to a table cell but when I replace the string with userdata.title
it shows up blank. userdata is an object of another class UserData, which is external and I imported it. UserData is accessed from all views to pass objects around from one controller to another. Now, how do I pass value to a table, and how do I keep it if I decide to add more values through my textfield?
- (void)viewDidLoad
{
tabledata = [[NSMutableArray alloc] initWithObjects:#"hello", nil];
tablesubtitles = [[NSMutableArray alloc] initWithObjects:#"hello", nil];
[super viewDidLoad];
//if I use "userdata.title" in replacemnent of #"hello" above, the table show up blank
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//--------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"homeworkcell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"homeworkcell"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
//static NSString *CellIdentifier = #"Cell";
/* if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}*/
// Configure the cell.
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:#"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--
return cell;
}
To pass your data from one ViewController (vc1 for example) to another (vc2), you can set a property it the destination controller. So for the UIViewController above, you should add in the .h
#property (nonatomic, retain) UserData *tableDatas;
Then, in the view controller vc1, you will have:
vc2.tableDatas = userdatas;
Finally, in the UITableViewDelegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you can just use:
cell.textLabel.text = [self.tableDatas objectAtIndex:indexPath.row];
There should be some adjustment but the main parts are here.
Good luck.