Tableview to Display Different Images on New Controller

TableView to display different images on new controller

You can make use of Default Delegate provided in TableView Conroller

 import UIKit

class CustomTableController: UITableViewController {

let adultcardiac = ["photo1", "photo2", "photo3"]

override func viewDidLoad()
{
super.viewDidLoad()
}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return adultcardiac.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "transportCell", for: indexPath)

cell.textLabel?.text = adultcardiac[indexPath.row]

return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let Vc = self.storyboard?.instantiateViewController(withIdentifier: "imageVC") as! imageVC

switch indexPath.row
{
case 0:
Vc.passedImage = UIImage.init(named: "screenShot")!
self.navigationController?.pushViewController(Vc, animated: true)
break;
case 1:
Vc.passedImage = UIImage.init(named: "screenShot1")!
self.navigationController?.pushViewController(Vc, animated: true)
break;
case 2:
Vc.passedImage = UIImage.init(named: "screenShot2")!
self.navigationController?.pushViewController(Vc, animated: true)
break;
default:
Vc.passedImage = UIImage.init(named: "screenShot")!
self.navigationController?.pushViewController(Vc, animated: true)
}
}
}

--> My imageVC Class

import UIKit

class imageVC: UIViewController {

@IBOutlet weak var myImageView: UIImageView!

var passedImage : UIImage! = nil

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

self.myImageView.image = passedImage
}
}

--> Output

---> When TableView Controller Loaded in memory Stack

Sample Image

--> When a Row is Selected

Sample Image

--> when DidSelect Execute and displays result - New ImageVc with passed Image

Sample Image

--> My StoryBoard

Sample Image

Getting to show images in tableview's cells on the next view controller

You should not be storing data in the cells of a table. The cells of a table are there to display information.

First, you seem to be using an iVar Carint to display different sets of cars. First, all variables should have lowercase first letters. Second, this is a bad idea.

By doing this you are strongly coupling the table to the data it is displaying. I'll ignore this for now though.

Second, you should also be using a custom UITableViewCell subclass to create UIImageViews etc... but I'll ignore this too.

Third, you should create a Class called Car. Then give the class Car two properties, name and image and actually you could give it type too but again I'll ignore this.

NOTE just because I am ignoring them doesn't mean you should too. Think of these first three as hints of what to work on next.

Fourth, create a function that will return the image for the row. Like this...

- (UIImage *)imageForRow:(NSUInteger)row
{
if (Carint == 0) {
return [UIImage imageNamed:[HatchbackImg objectAtIndex:indexPath.row]];
} else if (Carint == 1) {
return [UIImage imageNamed:[SUVImg objectAtIndex:indexPath.row]];
} else if (Carint == 2) {
return [UIImage imageNamed:[SedanImg objectAtIndex:indexPath.row]];
}

return nil;
}

and the title. Like this...

- (NSString *)titleForRow:(NSUInteger)row
{
if (Carint == 0) {
return [Hatchback objectAtIndex:indexPath.row];
} else if (Carint == 1) {
return [SUV objectAtIndex:indexPath.row];
} else if (Carint == 2) {
return [Sedan objectAtIndex:indexPath.row];
}

return nil;
}

This means that you can change your current methods like so...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}

cell.textLabel.text = [self titleForRow:indexPath.row];
cell.imageView.image = [self.imageForRow:indexPath.row];

return cell;
}

and...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *carName = [self titleForRow:indexPath.row];
UIImage *carImage = [self imageForRow:indexPath.row];

// Navigation logic may go here. Create and push another view controller.

// OMG! Fix the naming conventions. Class names should start with uppercase letters.
carSelectViewController *carDetailViewController = [[carSelectViewController alloc] initWithNibName:@"carSelectViewController" bundle:nil];

// These won't work until you do the next bit.
carDetailViewController.carName = carName;
carDetailViewController.carImage = carImage;

// Pass the selected object to the new view controller.
[self.navigationController pushViewController: carDetailViewController animated:YES];
}

Next, add properties to the "detail view controller" like this...

@interface carSelectViewController : UIViewController

@property NSString *carName;
@property UIImage *carImage;

@end

These are then being set by the previous view controller.

Finally, use the values of these properties to display the data.

- (void)viewDidLoad
{
self.title = self.carName;
imageView.image = self.carImage;

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

For more information about doing this and especially working on those first three points you can see here...

http://www.raywenderlich.com

How to put different images next to text in table view controller in Swift?

With the changes made to your code already, you need to set the image names for the other types, where I have marked.

class NearMeIndexTableViewController: UITableViewController  {
var nearMeIndexes = [NearMeIndexTitle]()

override func viewDidLoad() {
super.viewDidLoad()

// Table view list ..
let nearmeindex1 = NearMeIndexTitle()
let nearmeindex1Img = NearMeIndexTitle()

nearmeindex1.indexTitle = "Fast Food"
nearmeindex1.imageName = "fastfood.png"

let nearmeindex2 = NearMeIndexTitle()
nearmeindex2.indexTitle = "Dining"
nearmeindex2.imageName = "dining.png" // here

let nearmeIndex3 = NearMeIndexTitle()
nearmeIndex3.indexTitle = "Service Stations"
nearmeindex3.imageName = "stations.png" // here

let nearmeIndex4 = NearMeIndexTitle()
nearmeIndex4.indexTitle = "Hospitals "
nearmeindex4.imageName = "hospitals.png" // here

let nearmeIndex5 = NearMeIndexTitle()
nearmeIndex5.indexTitle = "Grocery"
nearmeindex5.imageName = "grocery.png" // here

nearMeIndexes.append(nearmeindex1)
nearMeIndexes.append(nearmeindex2)
nearMeIndexes.append(nearmeIndex3)
nearMeIndexes.append(nearmeIndex4)
nearMeIndexes.append(nearmeIndex5)

self.tableView.reloadData()
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NearMeIndexCell", for: indexPath) as! NearMeTableViewCell

let nearMendexCellValue = nearMeIndexes[indexPath.row]
cell.nearMeLabel.text = nearMendexCellValue.indexTitle

cell.nearMeImageView.image = UIImage(named: nearMendexCellValue.imageName1)

return cell
}
}

image view with table view in the same view controller

To have a logo type view you either need to set a custom headerview for the tableview via

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

and

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

the other method would be overriding -loadView and creating your own view that has two subviews, your imageview and a tableview.

In the first method, once your scroll some the logo will eventually disappear. The second method makes the logo static.

How to Load Different Images On Table View Cell

You can modified saveImage and loadImage function as per below.

-(void)saveImage: (UIImage*)image filePath:(NSString*)fileNameEmail
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileNameEmail];

NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:filePath atomically:YES];

}
}

- (UIImage*)loadImage:(NSString*)fileNameEmail
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileNameEmail];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
return image;
}

Edit : You can get specific image like below.

-(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

cell.youimageview.image = [self loadImage:[[yourArray indexOfObject:indexPath.row] valueForKey:@"youemailkey"]];
return cell;
}

may this help you.

How to add an image above tableview in TableViewController - Swift 3

Add UIImgaeView in HeaderView of that TableView.
Something like that -

    let headerImageView = UIImageView.init(frame: Frame)

headerImageView.image = Image

self.tableView.tableHeaderView = headerImageView

Xcode how to link UITableView Cells to a new View Controller

In didSelectRowAtIndexPath you can just init another view controller and present. You can present it from self.navigationController so that there is a back button if you wish. Here I show it presented modally:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Deselect row
[tableView deselectRowAtIndexPath:indexPath animated:YES];

// Declare the view controller
UIViewController *anotherVC = nil;

// Determine the row/section on the tapped cell
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0: {
// initialize and allocate a specific view controller for section 0 row 0
anotherVC = [[ViewControllerForRowZeroSectionZero alloc] init];
break;
}
case 1: {
// initialize and allocate a specific view controller for section 0 row 1
anotherVC = [[ViewControllerForRowOneSectionZero alloc] init];
break;
}
}
break;
case 1: {
// initialize and allocate a specific view controller for section 1 ALL rows
anotherVC = [[ViewControllerForAllRowsSectionOne alloc] init];
break;
}
}

// Get cell textLabel string to use in new view controller title
NSString *cellTitleText = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];

// Get object at the tapped cell index from table data source array to display in title
id tappedObj = [sitesArray objectAtIndex:indexPath.row];

// Set title indicating what row/section was tapped
[anotherVC setTitle:[NSString stringWithFormat:@"You tapped section: %d - row: %d - Cell Text: %@ - Sites: %@", indexPath.section, indexPath.row, cellTitleText, tappedObj]];

// present it modally (not necessary, but sometimes looks better then pushing it onto the stack - depending on your App)
[anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];

// Have the transition do a horizontal flip - my personal fav
[anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

// The method `presentModalViewController:animated:` is depreciated in iOS 6 so use `presentViewController:animated:completion:` instead.
[self.navigationController presentViewController:anotherVC animated:YES completion:NULL];

// We are done with the view controller. It is retained by self.navigationController so we can release it (if not using ARC)
[anotherVC release], anotherVC = nil;
}


Related Topics



Leave a reply



Submit