How to pass a UIImageView from UITableView (custom cell) to DetailViewController? - ios

I'm tearing my hair out over this one. I'm trying to pass an image URL from my TableViewController to my DetailViewController (FullArticleViewController) so that I can set the UIImageView, and nothing I try seems to be working. See my code below:
MyTableViewController.h
#interface MyTableViewController : UIViewController <UISearchBarDelegate>{
IBOutlet UITableView *DoctorsTableView;
NSArray *Doctors;
NSMutableData *data;
NSArray *searchResults;
}
#property (strong, nonatomic) NSString *cellImageLink;
#property (strong, nonatomic) UINavigationBar *navigationBar;
MyTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = #"DoctorsCell";
DoctorsCell *cell = (DoctorsCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DoctorsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(#"Using the search results");
cell.firstnameLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"node_title"];
cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"Opening Paragraph"];
NSString *firstLink = [[NSString alloc] init];
firstLink = [[[searchResults objectAtIndex:indexPath.row] objectForKey:#"Image"] objectForKey:#"filename"];
NSString *secondLink = [[NSString alloc] init];
secondLink = [NSString stringWithFormat:#"URL HERE%#",firstLink];
NSLog(#"second link is %#", secondLink);
cellImageLink = secondLink;
[cell.featureImage sd_setImageWithURL:[NSURL URLWithString:secondLink]];
} else {
NSLog(#"Using the Full List!");
cell.firstnameLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:#"node_title"];
cell.descriptionLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:#"Opening Paragraph"];
NSString *firstLink = [[NSString alloc] init];
firstLink = [[[Doctors objectAtIndex:indexPath.row] objectForKey:#"Image"] objectForKey:#"filename"];
NSString *secondLink = [[NSString alloc] init];
secondLink = [NSString stringWithFormat:#"URL HERE%#",firstLink];
NSLog(#"second link is %#", secondLink);
cellImageLink = secondLink;
[cell.featureImage sd_setImageWithURL:[NSURL URLWithString:secondLink]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FullArticleViewController *detailViewController = [[FullArticleViewController alloc]
initWithNibName:#"FullArticleViewController" bundle:nil];
if ([searchResults count]) {
detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"node_title"];
detailViewController.articleDetail = [searchResults objectAtIndex:indexPath.row];
} else {
detailViewController.title = [[Doctors objectAtIndex:indexPath.row] objectForKey:#"node_title"];
detailViewController.articleDetail = [Doctors objectAtIndex:indexPath.row];
NSLog(#"%#", Doctors);
}
FullArticleViewController *viewController = [[FullArticleViewController alloc]
initWithNibName:#"DetailViewController"
bundle:nil];
viewController.featureImage = searchResults[indexPath.row][#"Image"][#"filename"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
FullArticleViewController.h (detailview)
#interface FullArticleViewController : UIViewController
{
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *firstnameLabel;
IBOutlet UILabel *descriptionLabel;
IBOutlet UILabel *bodyLabel;
}
#property (nonatomic, copy) NSDictionary *articleDetail;
#property (strong, nonatomic) IBOutlet UIImageView *featureImage;
-(IBAction)goBack:(id)sender;
FullArticleViewController.m (detailview)
#import "SDWebImage/UIImageView+WebCache.h"
#import "FullArticleViewController.h"
#import "DoctorsCell.h"
#import "MyTableViewController.h"
#interface FullArticleViewController ()
#end
#implementation FullArticleViewController
#synthesize articleDetail;
#synthesize featureImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
featureImage = [[UIImageView alloc] init];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 5000)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
firstnameLabel.text = [articleDetail objectForKey:#"node_title"];
descriptionLabel.text = [articleDetail objectForKey:#"Opening Paragraph"];
bodyLabel.text = [articleDetail objectForKey:#"Body"];
}

MyTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FullArticleViewController *detailViewController = [[FullArticleViewController alloc]
initWithNibName:#"FullArticleViewController" bundle:nil];
if ([searchResults count]) {
detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"node_title"];
detailViewController.articleDetail = [searchResults objectAtIndex:indexPath.row];
detailViewController.cellImageLink = searchResults[indexPath.row][#"Image"][#"filename"];
} else {
detailViewController.title = [[Doctors objectAtIndex:indexPath.row] objectForKey:#"node_title"];
detailViewController.articleDetail = [Doctors objectAtIndex:indexPath.row];
detailViewController.cellImageLink = Doctors[indexPath.row][#"Image"][#"filename"];
}
NSLog(#"cellImageLink = %#", detailViewController.cellImageLink);
[self.navigationController pushViewController:detailViewController animated:YES];
}
FullArticleViewController.m
Set the image in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(#"cellImageLink = %#", self.cellImageLink);
featureImage = [[UIImageView alloc] init];
//maybe set the frame here?
[self.featureImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://domainnamehere.com/%#",cellImageLink]]];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 5000)];
firstnameLabel.text = [articleDetail objectForKey:#"node_title"];
descriptionLabel.text = [articleDetail objectForKey:#"Opening Paragraph"];
bodyLabel.text = [articleDetail objectForKey:#"Body"];
}

Typically you would set detail view properties upon cell selection right before pushing the new view controller. Something like this would do it:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
FullArticleViewController *detailView = [[FullArticleViewController alloc] init];
detailView.articleDetail = [searchResults objectAtIndex:indexPath.row];
[self.navigationController pushViewController detailView animated:YES];
}
Notice I am using the default initializer for your view controller and am also assuming you are using a UINavigationController. You may need to change one or both of these to fit your code.
Then, you would want to use the same SDWebImage category to set the image url in viewDidLoad of FullArticleViewController.m. It will probably look something like this:
- (void)viewDidLoad
{
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 5000)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
firstnameLabel.text = [articleDetail objectForKey:#"node_title"];
descriptionLabel.text = [articleDetail objectForKey:#"Opening Paragraph"];
bodyLabel.text = [articleDetail objectForKey:#"Body"];
[self.featureImage sd_setImageWithURL::[NSURL URLWithString:[[self.articleDetail objectForKey:#"Image"] objectForKey:#"filename"]];
}

Related

UISearchBar Text Not Updating List of File Names

In my class for my TableView, I populate the rows with the names of all files within a given folder of my app. I am just now changing it over to UISearchBar, and I'm having some issues with my logic. The search bar appears, but the tableview simply goes blank.
In my .h file I have:
#interface DevoSongs : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchResultsUpdating >{
NSArray *files;
NSIndexPath *thepath;
NSString *filenames;
NSArray *try2;
NSMutableArray *searchResults;
NSString *savedSearchTerm;
IBOutlet UITableView *tableView;
}
#property (nonatomic, retain) UITableView *tableView;
#property (strong, nonatomic) UISearchController *searchController;
#property (nonatomic, retain) NSArray *files;
#property (nonatomic, retain) NSString *filenames;
#property (nonatomic, retain) NSIndexPath *thepath;
#property (nonatomic, retain) NSArray *try2;
#property (nonatomic, retain) NSMutableArray *searchResults;
#property (nonatomic, copy) NSString *savedSearchTerm;
- (void)handleSearchForTerm:(NSString *)searchTerm;
#end
For my implementation:
-(void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchController alloc]
initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
[self.searchController.searchBar sizeToFit];
}
- (void)viewWillAppear:(BOOL)animated {
NSBundle *bundle = [NSBundle mainBundle];
self.files = [bundle pathsForResourcesOfType:#"pdf" inDirectory:#"thepdfpowerpoints"];
NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
self.title = #"Devo Songs";
self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
for (NSString *path in self.files) {
[names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
}
self.files = [names sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor darkGrayColor];
self.view.backgroundColor = [UIColor grayColor];
[super viewWillAppear:animated];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (self.searchController.isActive) {
return [searchResults count];
}
else {
return [self.files count];
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *filename = [[[self.files objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension];
NSInteger row = [indexPath row];
NSString *contentForThisRow = nil;
contentForThisRow = filename;
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
[[cell textLabel] setText:contentForThisRow];
cell.textLabel.font = [UIFont fontWithName:#"Roboto-Bold" size:38];
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor blackColor];
return cell;
}
else {
[[cell textLabel] setText:contentForThisRow];
cell.textLabel.font = [UIFont fontWithName:#"Roboto-Bold" size:22];
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor darkGrayColor];
return cell;
}
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
[self updateSearchResultsForSearchController:self.searchController];
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchString = self.searchController.searchBar.text;
NSPredicate *resultPredicate;
resultPredicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#",searchString];
searchResults = [self.files filteredArrayUsingPredicate:resultPredicate];
NSLog(#"results %lu", (unsigned long)[self.files count]);
[self.tableView reloadData];
}
Remove below code or comment below
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
[self updateSearchResultsForSearchController:self.searchController];}
Added the another searchController delegate method in your called show below.
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange (NSRange)range replacementText:(NSString *)text {
[self updateSearchResultsForSearchController:self.searchController];}
This should work. Hope it help :)

Global array with custom objects is not keeping objects' state

I am creating a bucket list app and I need to be able to save each BucketListGoal from a UITextField in a UITableViewCell. Here is my relevant code below -
#interface BucketListGoalViewController () <UITableViewDataSource, UITableViewDelegate, CreateGoalTableViewCellDelegate, UITextFieldDelegate, UIPopoverPresentationControllerDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) NSArray *goals;
#property NSInteger path;
#end
#implementation BucketListGoalViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = #"Goals";
self.count = 0;
self.goals = [[NSArray alloc] init];
[self createNewGoal];
self.tableView.allowsSelection = NO;;
[self.tableView registerClass:[CreateGoalTableViewCell class]forCellReuseIdentifier:#"Cell"];
[self.tableView registerNib:[UINib nibWithNibName:#"CreateGoalTableViewCell" bundle:nil] forCellReuseIdentifier:#"Cell"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleCoordinatesFromBucketList:)
name:#"Coordinates"
object:nil];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
CreateGoalTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
[cell.textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
cell.tag = indexPath.row;
cell.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textField.tag = indexPath.row;
cell.textField.delegate = self;
BucketListGoal *goal = [self.goals objectAtIndex:indexPath.row];
goal.tag = indexPath.row;
if (goal.address != nil) {
[cell.locationButton setTitle:goal.address forState:UIControlStateNormal];
}
goal.name = cell.detailTextLabel.text;
cell.textField.placeholder = #"Write the description of your goal here.";
cell.numberLabel.text = [NSString stringWithFormat:#"%ld)", (long)indexPath.row + 1];
return cell;
}
-(void)createNewGoal {
BucketListGoal *goal = [BucketListGoal new];
NSMutableArray *copy = [self.goals mutableCopy];
[copy addObject:goal];
self.goals = copy;
[self.tableView reloadData];
}
-(void)textFieldDidChange:(UITextField *)textField{
NSMutableArray *copy = [self.goals mutableCopy];
BucketListGoal *goal = [self.goals objectAtIndex:textField.tag];
goal.name = textField.text;
[copy replaceObjectAtIndex:textField.tag withObject:goal];
self.goals = copy;
}
I create the goal from a bar button item at the top of the view. The issue is that self.goals is only keeping the goal.name of the latest item of the array. All the other previous items from the text fields continue being nil. What am I missing here?

Using TapGestureRecognizer with profileimage in TableViewController

Good afternoon,
I'm trying to implement a "see user profile" button which is going to show a new ViewController with the user information. I have implemented a TapGestureRecognizer over the image profile when the image is pressed (it's a TableViewController).
The main problem is when I press the user image it shows a wrong ProfileViewController because it seems that my TableViewController is not updating the #username when I press a different row (it's showing the latest profile I have seen).
I have tried following a lot of tutorials but it's not working in any case, because whenever I press the profile image it shows (and I have tested it) the wrong username.
What I have to do to update the username to the one I have pressed?
Find an image of my XCode: http://i.stack.imgur.com/FvNHg.jpg
Here is my TableViewController.m (it's in the #"segueprofile")
//
// CarTableViewController.m
// TableViewStory
//
#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarTableViewController.h"
#import "CarDetailViewController.h"
#import "OtherProfileUserViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <Security/Security.h>
#import "SSKeychainQuery.h"
#import "SSKeychain.h"
#import "SBJson.h"
#interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
#end
#interface CarTableViewController ()
#end
#implementation CarTableViewController
#synthesize carMakes = _carMakes;
#synthesize carModels = _carModels;
#synthesize carImages = _carImages;
#synthesize like = _like;
#synthesize likes = _likes;
#synthesize comments = _comments;
#synthesize username = _username;
#synthesize refuser = _refuser;
#synthesize profileImage = _profileImage;
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchJson];
[self.tableView reloadData];
// Initialize the refresh control.
self.refreshControl = [[UIRefreshControl alloc] init];
//self.refreshControl.backgroundColor = [UIColor blackColor];
//self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self
action:#selector(fetchJson)
forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden = YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_jsonArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)anIndexPath
{
static NSString *CellIdentifier = #"carTableCell";
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CarTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.likeButton.tag = anIndexPath.row;
// Configure the cell...
cell.makeLabel.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:#"id"];
cell.likes.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:#"likes"];
cell.comments.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:#"comments"];
cell.username.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:#"username"];
cell.refuser.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:#"user_ref"];
cell.modelLabel.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:#"user"];
NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:#"imagen"]];
[cell.carImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"imagen"] options:SDWebImageRefreshCached];
NSURL * imageURL2 = [NSURL URLWithString:[[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:#"image"]];
[cell.profileImage setImageWithURL:imageURL2
placeholderImage:[UIImage imageNamed:#"image"]
options:SDWebImageRefreshCached];
return cell;
}
-(void)fetchJson {
self.carModels = [[NSMutableArray alloc] init];
self.carMakes = [[NSMutableArray alloc] init];
self.carImages = [[NSMutableArray alloc] init];
self.likes = [[NSMutableArray alloc] init];
self.comments = [[NSMutableArray alloc] init];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString * urlString = [NSString stringWithFormat:#"http://webiste.com/service.php"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError *error;
[_jsonArray removeAllObjects];
_jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
NSString* imagen = [jsonObject objectForKey:#"imagen"];
[_carImages addObject:imagen];
NSDictionary * jsonObject2 = [_jsonArray objectAtIndex:i];
NSString* user = [jsonObject2 objectForKey:#"user"];
[_carMakes addObject:user];
NSDictionary * jsonObject3 = [_jsonArray objectAtIndex:i];
NSString* date = [jsonObject3 objectForKey:#"date"];
[_carModels addObject:date];
}
dispatch_async(dispatch_get_main_queue(), ^{
{
[self.tableView reloadData];
[self.refreshControl endRefreshing];
}});
}
);
//NSLog(#"LINK ==> %#", _jsonArray);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//NSIndexPath *indexPath = [self tableView indexPathForCell:sender];
//NSLog(#"LINK ==> %#", indexPath);
if ([segue.identifier isEqualToString:#"segueprofile"]) {
OtherProfileUserViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"username"];
}
if ([segue.identifier isEqualToString:#"segueprofile2"]) {
OtherProfileUserViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"username"];
}
if ([[segue identifier] isEqualToString:#"ShowCarDetails"])
{
CarDetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
detailViewController.carDetailModel = [[NSArray alloc]
initWithObjects:
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:#"date"],
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:#"id"],
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:#"imagen"],
nil];
}
}
- (void) alertStatus:(NSString *)msg :(NSString *) title
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
}
- (IBAction)plusOne:(UIButton *)sender {
NSLog(#"%ld",(long)sender.tag);
}
#end
Any help will be much appreciated.
Regards,

Segue not updated inside TableViewController

Good afternoon,
I have created a Segue in my TableViewController where the user can touch the user profile and it sends that username to a ProfileViewController, where the user information is loaded (posts, image, info...) (something like a Facebook wall).
The problem is when I touch one user, it displays everything correctly, but when I touch another profile image, it displays the same ProfileViewController again, like the username is not updated and it's send wrong.
When I touch anywhere on the screen and then I touch again the user profile, in that case it works, but I need to make this work in every case.
I'm going to post my TableViewController if that helps you to find the problem because it's almost working, the only thing that I need to fix is that username that is not updated (Tap Gesture Recognizer over the image).
The segues are at the end of the code.
TableViewController.m
//
// CarTableViewController.m
//
#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarTableViewController.h"
#import "CarDetailViewController.h"
#import "OtherProfileUserViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#implementation CarTableViewController
#synthesize carMakes = _carMakes;
#synthesize carModels = _carModels;
#synthesize carImages = _carImages;
#synthesize likes = _likes;
#synthesize comments = _comments;
#synthesize username = _username;
#synthesize refuser = _refuser;
#synthesize profileImage = _profileImage;
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchJson];
[self.tableView reloadData];
// Initialize the refresh control.
self.refreshControl = [[UIRefreshControl alloc] init];
//self.refreshControl.backgroundColor = [UIColor blackColor];
//self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self
action:#selector(fetchJson)
forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden = YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_jsonArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"carTableCell";
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CarTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.makeLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"id"];
cell.likes.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"likes"];
cell.comments.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"comments"];
cell.username.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"username"];
cell.refuser.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"user_ref"];
cell.modelLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"user"];
NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"imagen"]];
[cell.carImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"imagen"] options:SDWebImageRefreshCached];
NSURL * imageURL2 = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"image"]];
[cell.profileImage setImageWithURL:imageURL2
placeholderImage:[UIImage imageNamed:#"image"]
options:SDWebImageRefreshCached];
return cell;
}
-(void)fetchJson {
self.carModels = [[NSMutableArray alloc] init];
self.carMakes = [[NSMutableArray alloc] init];
self.carImages = [[NSMutableArray alloc] init];
self.likes = [[NSMutableArray alloc] init];
self.comments = [[NSMutableArray alloc] init];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString * urlString = [NSString stringWithFormat:#"http://website.com/service.php"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError *error;
[_jsonArray removeAllObjects];
_jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
NSString* imagen = [jsonObject objectForKey:#"imagen"];
[_carImages addObject:imagen];
NSDictionary * jsonObject2 = [_jsonArray objectAtIndex:i];
NSString* user = [jsonObject2 objectForKey:#"user"];
[_carMakes addObject:user];
NSDictionary * jsonObject3 = [_jsonArray objectAtIndex:i];
NSString* date = [jsonObject3 objectForKey:#"date"];
[_carModels addObject:date];
}
NSLog(#"carModels ==> %#", _jsonArray);
dispatch_async(dispatch_get_main_queue(), ^{
{
[self.tableView reloadData];
[self.refreshControl endRefreshing];
}});
}
);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segueprofile"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
OtherProfileUserViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"username"];
}
if ([segue.identifier isEqualToString:#"segueprofile2"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
OtherProfileUserViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"username"];
}
}
#end
Thanks in advance.
OK, here is one of the options.
1. Delete all existing segues (segueprofile and segueprofile2)
2. Connect your tableviewcontroller (important: not tableview but controller itself) with your details controller by push-segue and name it segueprofile3
3. Refactor your code in the following manner
#property (nonatomic, strong) NSIndexPath * selectedIndexPath;
...
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)anIndexPath {
self.selectedIndexPath = anIndexPath;
[self performSegueWithIdentifier:#"segueprofile3" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segueprofile3"]) {
OtherProfileUserViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [[_jsonArray objectAtIndex:selectedIndexPath.row] valueForKey:#"username"];
}
}
Another option. Place UIButton control right over avatar UIImageView. Create associated IBOutlet inside your CarTableViewCell class and make outlet connection.
#property (nonatomic, weak) UIButton * btnAvatar;
Next, modify CarTableViewController code
//1. declare new propery
#property (nonatomic, strong) id selectedProfile;
//2. implement future button tap handler
-(IBAction) btnAvatarDidPress:(id) sender {
int tag = ((UIButton *) sender).tag;
self.selectedProfile = [_jsonArray objectAtIndex:tag];
[self performSegueWithIdentifier:#"segueprofile3" sender:nil];
}
//Extend your cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"carTableCell";
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CarTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.makeLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"id"];
cell.btnAvatar.tag = indexPath.row;
... rest of method
}
then remove didSelectRowAtIndexPath method, and connect btnAvatar Touch Up Inside action to btnAvatarDidPress method inside your controller code.
That's all I hope I've forgot nothing.
here is your example, in prepareForSegue, get the cell which button is in:
id v = sender;
while (![(v= v.superView) isKindOfClass:[UITableViewCell class]]
&& v!=nil) ;
if (v==nil)
// no ancestor view is cell.
else{
// now v is the cell.
NSIndexPath * indexPath = [self.tableView indexPathForCell:v];
// do the left
}

IOS Auto resizing table cells w. expanding popdown menu (like tweetbot)

I know this is probably going to be one big headache, but it needs to be done. I finally got my tableview resizing cells as well as my label in such a way that all the text from each cell is displayed correctly.
NOTE: the comment & bump buttons are NOT the popdown menu I want to displayed when the cell is expanded , those 2 buttons should always be displayed , the menu i want to add would be a set of 4 buttons that would display under those buttons when the cell is expanded
But now I would like to add a pop down menu to each cell like tweetbot, shown below
But this is proving to be quite difficult due to how i am resizing the label using autolayout constraints. I came across this example of how to implement something similar to Tweetbot that seems pretty simple and practical on github here.
The problem is this example assumes all cells are the same size and that their is no dynamic sizing to the individual cells.
PublicFeedViewController.M
#import "PublicFeedViewController.h"
#import "FeedItemCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "InboxDetailViewController.h"
#import "SWRevealViewController.h"
#import "CommentsViewController.h"
#import "NSDate+TimeAgo.h"
#interface PublicFeedViewController (){
NSArray *NameLabel;
NSArray *StatusLabel;
NSMutableArray *feedArray;
}
#end
#implementation PublicFeedViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//The below code prompts the user for push notifications. If allowed, code in AppDelegate takes over and stores the token.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
// Do any additional setup after loading the view.
self.FeedTable.dataSource=self;
self.FeedTable.delegate=self;
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = #selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"foo": #"bar"};
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
[manager POST:#"http://" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(#"JSON: %#", responseObject);
self->feedArray = [responseObject objectForKey:#"feed"];
[self.FeedTable reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return feedArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier=#"Cell";
FeedItemCell *Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!Cell){
Cell = [[FeedItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSLog(#"FEED ARRAY: %#", self->feedArray);
NSDictionary *tempDictionary= [self->feedArray objectAtIndex:indexPath.row];
// Display recipe in the table cell
NSString *thumb_img = [tempDictionary objectForKey:#"thumb_img"];
NSString *thumb_path=#"http://";
NSString *thumb_url = [thumb_path stringByAppendingString:thumb_img];
Cell.NameLabel.text=[tempDictionary objectForKey:#"first_name"];
Cell.StatusLabel.text=[tempDictionary objectForKey:#"message"];
Cell.msg_id=[tempDictionary objectForKey:#"msg_id"];
//Cell.status=[tempDictionary objectForKey:#"message"];
Cell.StatusLabel.lineBreakMode=0;
Cell.StatusLabel.numberOfLines=0;
NSString *commentCount = [tempDictionary objectForKey:#"comment_count"];
NSString *commentButtonText =[NSString stringWithFormat:#"Comments ( %# )",commentCount];
[Cell.commentButton setTitle:commentButtonText forState: UIControlStateNormal];
NSString *bumpCount = [tempDictionary objectForKey:#"bump_count"];
NSString *bumpButtonText =[NSString stringWithFormat:#"Bumps ( %# )",bumpCount];
[Cell.bumpButton setTitle:bumpButtonText forState: UIControlStateNormal];
//[Cell.StatusLabel sizeToFit];
NSString *created_string=[tempDictionary objectForKey:#"created"];
double created_double = created_string.doubleValue;
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:created_double];
NSString *ago = [date timeAgo];
Cell.timeLabel.text=ago;
//Cell.DefaultImg.image = [UIImage imageNamed:#"buhz_mini_logo.png"];
[Cell.DefaultImg setImageWithURL:[NSURL URLWithString:thumb_url]
placeholderImage:[UIImage imageNamed:#".png"]];
return Cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Ideally you should do lazy loading so that instead of creating a new textView each time, you just reuse the same one.
UITextView *temp = [[UITextView alloc] initWithFrame:CGRectMake(82, 26, self.FeedTable.frame.size.width, 18)]; //This initial size doesn't matter
NSDictionary *tempDictionary= [self->feedArray objectAtIndex:indexPath.row];
NSString *status = [tempDictionary objectForKey:#"message"];
temp.font =[UIFont fontWithName:#"System" size:12];
temp.text = status;
[temp isHidden];
CGFloat textViewWidth = 218;
CGRect tempFrame = CGRectMake(82,26,textViewWidth,18); //The height of this frame doesn't matter.
CGSize tvsize = [temp sizeThatFits:CGSizeMake(tempFrame.size.width, tempFrame.size.height)]; //This calculates the necessary size so that all the text fits in the necessary width.
//Add the height of the other UI elements inside your cell
return tvsize.height + 70;
}
#end
CUSTOM FeedItemCell.M
#import "FeedItemCell.h"
#import "WYPopoverController/WYPopoverController.h"
#import "WYPopoverController/WYStoryboardPopoverSegue.h"
#import "CommentsViewController.h"
#import "NSDate+TimeAgo.h"
#interface FeedItemCell() <WYPopoverControllerDelegate>
{
WYPopoverController* commentsPopoverController;
}
- (IBAction)open:(id)sender;
- (void)close:(id)sender;
#end
#implementation FeedItemCell
#synthesize commentButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(IBAction)bump:(id)sender{
[self expand];
}
- (IBAction)open:(id)sender
{
[self showpopover:sender];
}
- (void)close:(id)sender
{
[commentsPopoverController dismissPopoverAnimated:YES];
commentsPopoverController.delegate = nil;
commentsPopoverController = nil;
}
-(void)expand
{
CGRect oldFrame = [self frame];
[self setFrame:CGRectMake( oldFrame.origin.x,
oldFrame.origin.y,
oldFrame.size.width,
oldFrame.size.height * 2)];
}
-(void)contract
{
CGRect oldFrame = [self frame];
[self setFrame:CGRectMake( oldFrame.origin.x,
oldFrame.origin.y,
oldFrame.size.width,
oldFrame.size.height / 2)];
}
- (IBAction)showpopover:(id)sender
{
if (commentsPopoverController == nil)
{
UIView *btn = (UIView*)sender;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
CommentsViewController *commentsViewController = [storyboard instantiateViewControllerWithIdentifier:#"Comments"];
commentsViewController.msg_id=_msg_id;
if ([commentsViewController respondsToSelector:#selector(setPreferredContentSize:)]) {
commentsViewController.preferredContentSize = CGSizeMake(300, 500); // iOS 7
}
else {
commentsViewController.contentSizeForViewInPopover = CGSizeMake(300, 500); // iOS < 7
}
commentsViewController.title = #"Comments";
[commentsViewController.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(close:)]];
commentsViewController.modalInPopover = NO;
UINavigationController* contentViewController = [[UINavigationController alloc] initWithRootViewController:commentsViewController];
commentsPopoverController = [[WYPopoverController alloc] initWithContentViewController:contentViewController];
commentsPopoverController.delegate = self;
commentsPopoverController.passthroughViews = #[btn];
commentsPopoverController.popoverLayoutMargins = UIEdgeInsetsMake(10, 10, 10, 10);
commentsPopoverController.wantsDefaultContentAppearance = NO;
[commentsPopoverController presentPopoverFromRect:btn.bounds
inView:btn
permittedArrowDirections:WYPopoverArrowDirectionNone
animated:YES
options:WYPopoverAnimationOptionFadeWithScale];
}
else
{
[self close:nil];
}
}
- (BOOL)popoverControllerShouldDismissPopover:(WYPopoverController *)controller
{
return YES;
}
- (void)popoverControllerDidDismissPopover:(WYPopoverController *)controller
{
if (controller == commentsPopoverController)
{
commentsPopoverController.delegate = nil;
commentsPopoverController = nil;
}
}
#end

Resources