I am new to ios programming, and I am trying to write a view controller that will display a question (via a label) at the top of the view and then a list of answers in a tableview subview below. The view is set up with interface builder, but I add the tableview programmatically.
I am able to load the data from a JSON source and print it out with a swipe gesture, but the data cannot be accessed inside my table view. I know from putting NSLog's that the numberOfRowsInSection runs once at the beginning to return 0 but does not run again when I invoke [self.tableView reloadData], which means the table still thinks it has 0 rows even though it should have about 20. I do set the UIViewController as the table view delegate and data source. What else do I need to do?
MAIN QUESTION: How can I access the view controller's NSMutablearray property inside my table view functions when that table view is a subview of my view controller? Thank you!
Here's my initial declarations and setup:
#interface BNRJudgeViewController () <UITextFieldDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate, UITableViewDataSource, UITableViewDelegate >
#property (nonatomic, strong) NSURLSession *session;
#property (weak, nonatomic) IBOutlet UILabel *question;
#property (weak, nonatomic) UITableView *tableView;
#property (nonatomic) NSMutableArray *answerArray;
#end
#implementation BNRJudgeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if ([self isKindOfClass:[UIViewController class]]) {
if(self){
// Custom initialization
self.title=#"Judge";
self.navigationController.navigationItem.title = #"Judge";
self.answerArray = [[NSMutableArray alloc] init];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil];
CGRect frame = CGRectMake(61, 252, 200, 225);
UITableView *tableView = [[UITableView alloc] initWithFrame:frame];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self isKindOfClass:[UIViewController class]]) {
[self fetchFeed];
}
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
rightSwipe.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
leftSwipe.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:leftSwipe];
}
Here's where I fetch the JSON data and assign it to an NSMutableArray property of my view controller:
- (void)fetchFeed
{
NSString *requestString = #"http://wsomeurl";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask =
[self.session dataTaskWithRequest:req completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error){
NSMutableArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
self.answerArray = jsonObject;
// NSLog(#"%#", self.answerArray);
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
[self.tableView reloadData];
[dataTask resume];
[self.tableView reloadData];
}
Here are my tableview required functions, implemented in my view controller:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"recent integer count is %d", [self.answerArray count]);
return [self.answerArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"UITableViewCell"];
// NSDictionary *cellD = self.answerArray[indexPath.row];
NSLog(#"%#", self.answerArray);
NSLog(#"hi hi!");
// NSLog(#"%#", [cellD objectForKey:#"username"]);
// cell.textLabel.text = [cellD objectForKey:#"username"];
return cell;
}
And finally here are some swipe functions showing that those functions can see the array - it's just the table view functions that aren't.
- (void)swipeRight:(UISwipeGestureRecognizer *) gr
{
NSLog(#"right swipe worked");
NSLog(#"right swipe array count is %d", [self.answerArray count]);
}
- (void)swipeLeft:(UISwipeGestureRecognizer *) gr
{
NSLog(#"left swipe worked");
NSLog(#"left swipe array count is %d", [self.answerArray count]);
}
Sorry for the long post. Again, MAIN QUESTION: How can I access the view controller's NSMutablearray property inside my table view functions? Thank you!
Extra technique related questions/comments...I recoded this making a custom tableviewcontroller class to load this data and have the right size and location and then initialize this tableview controller class within my view controller and put its view as a subview. But I don't want to be passing data between two view controllers because I'd like to update both the view and the subview with a swipe on the view as cleanly as possible...but might this be the better way to go?
Two related technique questions as well. First would it make more sense to stuff this information into a singleton class that I can access from any view controller? Second, would it make more sense to write some protocol (haven't done this, just know of it) to pass the information force reload in the right place?) As a newbie, I see both of these as sloppier than just sticking with one view controller as I'm trying to do, but I might just not understand objective c very well yet. Thanks in advance.
You have UITableView *tableView = [[UITableView alloc] initWithFrame:frame]; in your init method, so tableView is a local variable. Elsewhere you have [self.tableView reloadData], but you've never assigned your property to the table view, so self.tableView is nil. You should change the code in your init method to,
_tableView = [[UITableView alloc] initWithFrame:frame];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
Related
I have been trying to implement UISearchController by following stackoverflow thread:
How to implement UISearchController with objective c
and Apples's documentation, but can't make it working. When the initial controller (EVTSearchViewController which is defined as UIViewController) that complies with UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate
(I needed to make it compliant with UITableViewDelegate too, since I am making it a delegate of the other UITableViewController type EVTSearchResultsViewController *resultsControllers tableView) delegates is presented, I see my .xib with the UISearchBar and UITableView, I click the search bar and start typing:
When I type one letter, search bar disappears and nothing is shown:
First, I do not want the search bar to disappear. Second, updateSearchResultsForSearchController seems not to be called at all since NSLog() set up there does not produce any output when I am typing in the search bar.
I have .xib for the EVTSearchViewController and it has a UISearchBar that I am connecting to the respective property: IBOutlet UISearchBar *searchBar which is then made to point to the UISearchControllers' searchBar:
self.searchBar = self.searchController.searchBar
There is also UITableView that I put below the UISearchBar in the .xib. Another controller that I am using inside EVTSearchViewController is EVTSearchResultsViewController, it is UITableViewController and it does not have its .xib.
Below is the code for viewDidLoad and updateSearchResultsForSearchController methods:
- (void)viewDidLoad
{
_resultsController = [[EVTSearchResultsViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:_resultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
self.searchBar = self.searchController.searchBar;
// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = YES; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
//
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// update the filtered array based on the search text
NSString *searchText = searchController.searchBar.text;
NSLog(#"searchText: %#", searchText);
if (searchText == nil) {
// If empty the search results are the same as the original data
self.searchResults = [[[EVTItemStore sharedStore] allItems] mutableCopy];
} else {
NSMutableArray *searchResults = [[NSMutableArray alloc] init];
NSArray *allEvents = [[EVTItemStore sharedStore] allItems];
NSLog(#"allEvents: %#", allEvents);
for (EVTItem *event in allEvents) {
/*if ([event.number containsString:searchText] || [[phoneMO.closrr_id filteredId] containsString:searchText] || [[phoneMO.contact.fullname lowercaseString] containsString:[searchText lowercaseString]]) {
[searchResults addObject:phoneMO];
}*/
if ([event.eventName containsString:searchText]) {
[searchResults addObject:event];
}
}
self.searchResults = searchResults;
}
// hand over the filtered results to our search results table
EVTSearchResultsViewController *resultsController = (EVTSearchResultsViewController *)self.searchController.searchResultsController;
resultsController.filteredEvents = self.searchResults;
[resultsController.tableView reloadData];
}
The respective #properties defined in EVTSearchViewController:
#interface EVTSearchViewController ()
#property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
#property (nonatomic, strong) UISearchController *searchController;
#property (nonatomic, strong) EVTSearchResultsViewController *resultsController;
#property (nonatomic, strong) NSMutableArray *searchResults;
// For state restoration
#property BOOL searchControllerWasActive;
#property BOOL searchControllerSearchFieldWasFirstResponder;
#end
Then, here is the code for EVTSearchResultsViewController:
#import "EVTSearchResultsViewController.h"
#implementation EVTSearchResultsViewController
- (instancetype)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"UISearchViewCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.filteredEvents count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:#"UISearchViewCell"
forIndexPath:indexPath];
cell.textLabel.text = self.filteredEvents[indexPath.row];
return cell;
}
#end
The methods of EVTSearchResultsViewController above are not called at all which makes look very strange for me, why do we need it at all, then?
I tried to set UISearchBar the way Apple docs recommend:
self.resultsTableView.tableHeaderView = self.searchController.searchBar;
but it gives me a non-responsive search box, so when pressing nothing happens.
Could anyone, please, help with resolving the question. The other question linked above can also be clarified then. Thank you.
So, I solved the issue and implemented basic search with UISearchController. Here is what we need to do to implement the basic search:
Create two UITableViewControllers classes with NO .xib files. Yes, there should be no .xib files and we are just creating two classes. In the code below their names are EVTSearchViewController and EVTSearchResultsViewController.
Make one of the controllers to comply with the delegates: <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
Here is the code for the header file of EVTSearchViewController:
#import <UIKit/UIKit.h>
#interface EVTSearchViewController : UITableViewController <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating>
#end
Here is the EVTSearchResultsViewController's header:
#import <UIKit/UIKit.h>
#interface EVTSearchResultsViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *filteredEvents;
#end
The NSMutableArray *filteredEvents will hold the results of the search. We should NOT implement any of the UITableViewController delegate methods in EVTSearchViewController.m, but should in EVTSearchResultsViewController.m.
Here is the top part of EVTSearchViewController:
#import "EVTSearchViewController.h"
#import "EVTSearchResultsViewController.h"
// Importing the class that stores `EVTItem` object classes
#import "EVTItemStore.h"
#import "EVTItem.h"
#interface EVTSearchViewController ()
#property (nonatomic, strong) UISearchController *searchController;
// We created this class
#property (nonatomic, strong) EVTSearchResultsViewController *resultsController;
// array to hold the results of the search
#property (nonatomic, strong) NSMutableArray *searchResults;
#end
Here is the code for EVTSearchViewController's viewDidLoad: method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.resultsController = [[EVTSearchResultsViewController alloc] init];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
// This line of code is very important. Here we are using apple docs'
// suggestion. UITableViewController has tableView property, so
// we are just setting tableView`s header to apples' UISearchController`s' `searchBar property
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.delegate = self;
// default is YES
self.searchController.dimsBackgroundDuringPresentation = YES;
// so we can monitor text changes + other changes
self.searchController.searchBar.delegate = self;
// know where you want UISearchController to be displayed
self.definesPresentationContext = YES;
}
Then we add to EVTSearchViewController the following method:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// update filtered array based on the search text
NSString *searchText = searchController.searchBar.text;
if (searchText == nil) {
// If empty the search results should be the same as the original data
self.searchResults = [[[EVTItemStore sharedStore] allItems] mutableCopy];
} else {
NSMutableArray *searchResults = [[NSMutableArray alloc] init];
// [[EVTItemStore sharedStore] allItems] message retrieves
// all of the objects that I have in datastore EVTItemStore
NSArray *allEvents = [[EVTItemStore sharedStore] allItems];
// EVTItem class has a property eventName which we are using
// for searching, then adding it to our searchResults array
for (EVTItem *event in allEvents) {
if ([event.eventName containsString:searchText]) {
[searchResults addObject:event];
}
}
self.searchResults = searchResults;
}
// hand over the filtered results to our search results table
EVTSearchResultsViewController *resultsController = (EVTSearchResultsViewController *)self.searchController.searchResultsController;
resultsController.filteredEvents = self.searchResults;
[resultsController.tableView reloadData];
}
Another controller's EVTSearchResultsViewController #implementation part looks like this:
#implementation EVTSearchResultsViewController
- (instancetype)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.filteredEvents count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:#"UISearchViewCell"
forIndexPath:indexPath];
EVTItem *event = self.filteredEvents[indexPath.row];
cell.textLabel.text = event.eventName;
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"UISearchViewCell"];
}
#end
That's it. If we need to further customize our cells, we should be able to do it by making UISearchViewCell.xib for EVTSearchResultsViewController, and using the following viewDidLoad instead:
- (void)viewDidLoad
{
[super viewDidLoad];
// Load the NIB file
UINib *nib = [UINib nibWithNibName:#"UISearchViewCell" bundle:nil];
// Register this NIB which contains the cell
[self.tableView registerNib:nib forCellReuseIdentifier:#"UISearchViewCell"];
}
Trying to load a url being passed from the previous viewcontroller in a tabviewcontroller sequence. Checked the usual suspects. UIWebview is added view outlet. All the proper delegate are connected. I'm not sure if the NSURL is being passed to the next view controller as I get a black screen (although the response object isn't nil). It's driving me crazy because it's a simple problem and it's something I've done millions of times but it's not working. Any help would be appreciated.
Below you'll find the code.
SquirrelInformationTableViewController.m
#interface ASAInformationTabViewController (){
NSMutableArray *squirrelArray;
}
#property (nonatomic, strong) ODRefreshControl *refreshControl;
#property (weak, nonatomic) IBOutlet UITableView *squirrelListTableView;
#end
#implementation ASAInformationTabViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpSquirrelList];
//Add drag to refresh function
self.refreshControl = [[ODRefreshControl alloc]initInScrollView:self.squirrelListTableView];
self.refreshControl.tintColor = [UIColor colorWithRed:46.0/255 green:172.0/255 blue:247.0/255 alpha:1];
[self.refreshControl addTarget:self action:#selector(dragToRefreshAction:) forControlEvents:UIControlEventValueChanged];
}
-(void)setUpSquirrelList
{
__weak UITableView* weaktable = self.squirrelListTableView;
[[ASASquirrelManager instance] listSquirrels:^(NSString *response, NSMutableArray *result) {
if ([response isEqualToString:CODE_SUCCESS]) {
squirrelArray = result;
dispatch_async(dispatch_get_main_queue(), ^{
if (weaktable) {
[weaktable reloadData];
}
});
}
}];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ASASquirrelInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"squirrelIdentifier"];
ASASquirrelModel *squirrelLocal = squirrelArray[indexPath.row];
cell.squirrelTitleLabel.text = squirrelLocal.title;
cell.squirrelDescLabel.text = squirrelLocal.desc;
NSData *imageData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:squirrelLocal.image]];
cell.squirrelImage.image = [UIImage imageWithData:imageData];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ASASquirrelModel *squirrelWebLocal = squirrelArray[indexPath.row];
NSString *squirrelURLString = squirrelWebLocal.url;
//create an array with the string you want an access it
NSLog(#"%#", urlStringArray);
//Create a string from the URL pass it to the sdvc.string
SquirrelDetailViewController *sdvc = [SquirrelDetailViewController new];
sdvc.urlString = squirrelURLString;
[self.navigationController pushViewController:sdvc animated:YES];
}
SquirrelDetailViewController.m
#interface SquirrelDetailViewController () <UIWebViewDelegate>
#end
#implementation SquirrelDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.frame = self.view.bounds;
[self.view addSubview:self.webView];
}
-(void)viewWillAppear:(BOOL)animated
{
self.urlstr = [NSURL URLWithString:self.urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.urlstr];
[self.webView loadRequest:requestObj];
}
I think you need to create the instance of the sdvc using storyboard otherwise it will not be getting instantiated properly. So:
SquirrelDetailViewController *sdvc =
(SquirrelDetailViewController *)[[UIStoryboard storyboardWithName:#"XXX" bundle:nil] instantiateViewControllerWithIdentifier:#"YYY"];
where XXX is your storyboard name and YYY is the storyboard identifier for SquirrelDetailViewController.
I found out that the trouble was I wasn't preparing for segue. (hits forehead*)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"webviewpush" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"webviewpush"]) {
// Get destination view
SquirrelDetailViewController *vc = [segue destinationViewController];
NSIndexPath *path = [self.squirrelListTableView indexPathForSelectedRow];
ASASquirrelModel *squirrelWebLocal = squirrelArray[path.row];
NSString *squirrelURLString = squirrelWebLocal.url;
vc.urlString = squirrelURLString;
}
}
If you'll excuse me I'm going to go drown myself now.
I'm trying to create a table but none of the data I inputted is displaying when I run the simulator. I'm trying to create a simple address book application and So far, here is what I did:
From the empty story board, I added a View Controller from the Object Library then inside the view controller, I added a Table View. Then I embedded the View Controller inside a Navigation Controller. I also added another View Controller in the end to act as a "Details" View.
Inside my viewController.h I added the following code:
#property (strong, nonatomic) NSArray *contact;
#property (strong, nonatomic) NSArray *contactDetails;
#property (nonatomic) NSInteger selectedIndex;
contact is to act as the array that shows all the contacts within the phonebook (This is what I'm having trouble displaying)
contactDetails shows once a user selects a contact.
selectedIndex is my way of telling the application which contact has been selected.
Inside my viewController.m I added the following code:
#import "HWViewController.h"
#interface HWViewController ()
#end
#implementation HWViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView registerClass: [UITableViewCell class] forCellReuseIdentifier:#"Cell"];
self.contact = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects:#"Joe", #"Simon", nil]];
self.contactDetails = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects: #"+689171898057", #"+689173104153", nil]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (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 self.contact.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if(cell==nil){
cell = [[[NSBundle mainBundle] loadNibNamed:#"SampleCell" owner:nil options:nil] objectAtIndex:0];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
label.text = [self.contact objectAtIndex:indexPath.row];
[cell.contentView addSubview:label];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.selectedIndex = indexPath.row;
[self performSegueWithIdentifier:#"TranslationSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
UIViewController *vc = [segue destinationViewController];
UILabel *details = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 250, 32)];
details.text = [self.contactDetails objectAtIndex:((HWViewController*)sender).selectedIndex];
[vc.view addSubview:details];
}
#end
After that, I ran the simulation and I get nothing showing up on the table.
you have no cell in the tableview. so add tableview cell in your storyboard
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Add this in your viewController.h :
#property(nonatomic, strong) IBOUTLET UITableView * TableView;
And then link this outlet to your tableview and then try it out.
And there is no cell in your table view because the prototype cell will be set to zero by default. Now select the tableview go to attribute inspector and set the prototype cell to 1 and then try to run your program with proper cell identifier it will work.
HTH :)
Set tableView delegate and DataSource protocol in you viewdidload method.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView registerClass: [UITableViewCell class] forCellReuseIdentifier:#"Cell"];
self.tableview.delegate = self;
self.tableview.dataSource = self;
self.contact = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects:#"Joe", #"Simon", nil]];
self.contactDetails = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects: #"+689171898057", #"+689173104153", nil]];
}
Currently I have custom UITablViewCell's (BIDSelectBusinessCustomCell) that are displayed in a tableView (myTableView). The custom cell is made up of a UIImageView and a label. When the view loads the labels are populated with the strings from my model. UIImageViews are blank. I want to be able for a user to 'tap' the UIImageView, select a picture from what is stored on their phone and for that image to be saved to the UIImageView.
From the below code I can get the 'tap' gesture, then the pickercontroller pops up and a user selects an image. The way the code is now one image that is selected is set for all of the UIImageViews. Which is understandable. But I want it to be set to that particular UIImageView and don't know how to.
Code:
#import <UIKit/UIKit.h>
#interface BIDBusinessSelectViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (strong, nonatomic) NSArray *linkedBusinessParseArray;
//Stores the array of models
#property (strong, nonatomic) NSMutableArray *linkedBusinessParseModelArray;
#property NSUInteger relevantIndex;
#property (strong, nonatomic) UIImage *tempImageHolder;
#end
#import "BIDBusinessSelectViewController.h"
#import <Parse/Parse.h>
#import "BIDBusinessModel.h"
#import "BIDSelectBusinessCustomCell.h"
#interface BIDBusinessSelectViewController () <ImageSelect>
{
BIDSelectBusinessCustomCell *aCell;//define a cell of ur custom cell to hold selected cell
UIImage *choosenImage; //image to set the selected image
}
#end
#implementation BIDBusinessSelectViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.linkedBusinessParseModelArray = [[NSMutableArray alloc]init];
//create query
PFQuery *linkedBusinessParseQuery = [PFQuery queryWithClassName:#"linkedBusinessParseClass"];
//follow relationship
[linkedBusinessParseQuery whereKey:#"currentBusinessUserParse" equalTo:[PFUser currentUser]];
[linkedBusinessParseQuery whereKey:#"linkRequestSentParse" equalTo:#"Approved"];
[linkedBusinessParseQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.linkedBusinessParseArray = objects; //store them in my model array
//loop through model array from Parse
for (NSDictionary *dict in self.linkedBusinessParseArray) {
NSString *descPlaceNameParse = [dict objectForKey:#"placeDescriptionParse"];
NSLog(#"descPlacesNameParse: %#",descPlaceNameParse);
PFObject *tempObj = (PFObject *) dict;
NSString *tempObjString = tempObj.objectId;
NSLog(#"tempObjString (inside dict): %#", tempObjString);
//storing values from Parse into my model
BIDBusinessModel *userModel = [[BIDBusinessModel alloc]init];
userModel.descriptionModelParse = descPlaceNameParse;
userModel.objectIdModelParse = tempObjString;
[self.linkedBusinessParseModelArray addObject:userModel];
NSLog(#"self.linkedBusinessParseModelArray: %lu", (unsigned long)[self.linkedBusinessParseModelArray count]);
//Reload tableview. Has to go here in block otherwise it does not occur
[self.myTableView reloadData];
}
}];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
choosenImage = [UIImage imageNamed:#"pin.png"]; //hear u need to set the image for cell assuming that u are setting initially same image for all the cell
}
#pragma mark -
#pragma mark Table Delegate Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.linkedBusinessParseModelArray.count; //returns count of model NSMutableArray
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Start cellforRowStIndex");
static NSString *CellIdentifier = #"Cell";
BIDSelectBusinessCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[BIDSelectBusinessCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
BIDBusinessModel *bizModel;
bizModel = self.linkedBusinessParseModelArray[indexPath.row];
bizModel.image = choosenImage;
//cell.descLabel.text = [NSString stringWithFormat:#"bid= %d",indexPath.row];//set text from the model//Omitted for my desc
cell.descLabel.text = bizModel.descriptionModelParse;
cell.logoImage.image =bizModel.image; //setting the image initially the image when u set in "viewDidLoad" method from second time onwords it will set from the picker delegate method
//insted of settig the gesture hear set it on the custom cell
cell.ImageSelectDelegate = self; //setting the delegate
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
// hear implementation of delegate method
- (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView;
{
//open up the image picker
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
aCell = (BIDSelectBusinessCustomCell *)logoImgView.superview; //her getting the cell
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *aChosenImage = info[UIImagePickerControllerEditedImage];
//UIImage holder to transfer to cellForRowAtIndexPath
choosenImage = aChosenImage;
NSIndexPath *indexPath = [self.myTableView indexPathForCell:aCell];
[self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //hear reloading the selected cell only not entire tableview
//get the model and set the choosen image
BIDBusinessModel *bizModel;
bizModel = self.linkedBusinessParseModelArray[indexPath.row];
bizModel.image = aChosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
#import <UIKit/UIKit.h>
#protocol ImageSelect<NSObject> //for this u need use custom delegate so i did like this
- (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView;
#end
#interface BIDSelectBusinessCustomCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *descLabel;
#property (strong, nonatomic) IBOutlet UIImageView *logoImage;
#property (nonatomic, assign) id<ImageSelect>ImageSelectDelegate; // deleagte
#end
#import "BIDSelectBusinessCustomCell.h"
#implementation BIDSelectBusinessCustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.logoImage = [[UIImageView alloc]init];
self.descLabel = [[UILabel alloc]init];
//set up gesture hear in the custom cell insted in the controller class
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapLogoImage:)];
[tap setNumberOfTouchesRequired:1];
[tap setNumberOfTapsRequired:1];
[tap setDelegate:self];
self.logoImage.userInteractionEnabled = YES;
[self.logoImage addGestureRecognizer:tap];
//[self addSubview:logoImage];
//[self addSubview:descLabel];
}
return self;}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)tapLogoImage:(UITapGestureRecognizer *)guesture
{
if([self.ImageSelectDelegate respondsToSelector:#selector(selectSetImageForSelectedLogImage:)])
{
[self.ImageSelectDelegate selectSetImageForSelectedLogImage:self.logoImage];//call the delegate method from the selected cell
}
}
#end
Yup for this, i tried but finally i did something like this, go through this u may find some solution. and hope this helps u :)
//in custom cell "BIDSelectBusinessCustomCell.h"
#import <UIKit/UIKit.h>
//define a delegate method that is called each time when u tapped on cell
#protocol ImageSelect<NSObject> //for this u need use custom delegate so i did like this
- (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView;
#end
#interface BIDSelectBusinessCustomCell : UITableViewCell <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImageView *logoImage; //your image holder
UILabel *descLabel; //label
}
#property (nonatomic, retain) UIImageView *logoImage;
#property (nonatomic, retain) UILabel *descLabel;
#property (nonatomic, assign) id<ImageSelect>ImageSelectDelegate; // deleagte
#end
//in custom cell "BIDSelectBusinessCustomCell.m"
#import "BIDSelectBusinessCustomCell.h"
#implementation BIDSelectBusinessCustomCell
#synthesize logoImage;
#synthesize descLabel;
#synthesize ImageSelectDelegate;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
logoImage = [[UIImageView alloc]init];
descLabel = [[UILabel alloc]init];
//set up gesture hear in the custom cell insted in the controller class
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapLogoImage:)];
[tap setNumberOfTouchesRequired:1];
[tap setNumberOfTapsRequired:1];
[tap setDelegate:self];
logoImage.userInteractionEnabled = YES;
[logoImage addGestureRecognizer:tap];
[self addSubview:logoImage];
[self addSubview:descLabel];
}
return self;
}
- (void)dealloc
{
[logoImage release];
[descLabel release];
[super dealloc];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
//in this method simply i am setting the frames of subviews
[super layoutSubviews];
self.logoImage.frame = CGRectMake(130, 2, 44,44 );
self.descLabel.frame = CGRectMake(20, 2, 60, 44);
}
- (void)tapLogoImage:(UITapGestureRecognizer *)guesture
{
if([self.ImageSelectDelegate respondsToSelector:#selector(selectSetImageForSelectedLogImage:)])
{
[self.ImageSelectDelegate selectSetImageForSelectedLogImage:self.logoImage];//call the delegate method from the selected cell
}
}
#end
//in your view controller class u need to do something like this ;)
//ViewController.h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (retain, nonatomic) IBOutlet UITableView *aTableView;
#property (nonatomic, retain) NSIndexPath *selectedIndexPath; //no need just try
#end
//in your "ViewController.m" file
#interface ViewController ()<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControll erDelegate,ImageSelect>//delegate
{
BIDSelectBusinessCustomCell *aCell;//define a cell of ur custom cell to hold selected cell
UIImage *choosenImage; //image to set the selected image
}
- (void)viewDidLoad
{
[super viewDidLoad];
choosenImage = [UIImage imageNamed:#"Star.png"]; //hear u need to set the image for cell assuming that u are setting initially same image for all the cell
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[aTableView release];
[super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4; //pass array check array contains valid objects
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BIDSelectBusinessCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil)
{
cell = [[BIDSelectBusinessCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
BIDBusinessModel *bizModel;
bizModel = self.linkedBusinessParseModelArray[indexPath.row];
bizModel.image = aChosenImage;
cell.descLabel.text = [NSString stringWithFormat:#"bid= %d",indexPath.row];//set text from the model
cell.logoImage.image =bizModel.image; //setting the image initially the image when u set in "viewDidLoad" method from second time onwords it will set from the picker delegate method
//insted of settig the gesture hear set it on the custom cell
cell.ImageSelectDelegate = self; //setting the delegate
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
// hear implementation of delegate method
- (void)selectSetImageForSelectedLogImage:(UIImageView *)logoImgView;
{
//open up the image picker
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
aCell = (BIDSelectBusinessCustomCell *)logoImgView.superview; //her getting the cell
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *aChosenImage = info[UIImagePickerControllerEditedImage];
//UIImage holder to transfer to cellForRowAtIndexPath
choosenImage = aChosenImage;
NSIndexPath *indexPath = [aTableView indexPathForCell:aCell];
[self.aTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //hear reloading the selected cell only not entire tableview
//get the model and set the choosen image
BIDBusinessModel *bizModel;
bizModel = self.linkedBusinessParseModelArray[indexPath.row];
bizModel.image = aChosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Yes it is true this would set the image to every row..
Now what you can do is : -
While loading of tableViewCells just assign the tag of UIImageView as the (indexPath.Row).
This would assign unique tags to the ImageView.
You can easily get the tag of that imageView while tapping and now you can assign the image to particular imageView through tagValue of that imageView.
This would assign image to a particular imageView as all have unique tags.
Hope you got the answer of your problem.
I have a problem with refreshing the content of a UITableView. I'm trying to display a list of members in a UITableView. Above this list I have some kind of dropdown-menu for a group selection. So, if I change the group, selected in the dropdown-menu, I want to change the content of the UITableView underneath it. It's actually not working correctly.
The first time I load the ViewController that contains the UITableView, it shows the correct list of members in the default group. Now, when I change the group, it should display my new list of members, but it doesn't. It seems that the UITableView would not even reload its table content.
Here is my code for the TableViewController:
MemberTableViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.members = [NSMutableArray new];
self.selectedRows = [NSMutableArray new];
self.groupManager = [GroupManager new];
int *number;
if (nil != [self gruppeId]) {
number = [[self gruppeId] intValue]; // Here will be the id of the new selected group
} else {
number = 4010292; // This is just for testing. It's the id of my default group
}
for (Group *group in [self.groupManager findGroupWithID:[NSNumber numberWithInt:number]]) {
for (Member *member in group.member) {
[self.members addObject:member];
}
}
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES selector:#selector(caseInsensitiveCompare:)];
[self.gruppenMitglieder sortUsingDescriptors:[NSArray arrayWithObject:sort]];
[self.tableView setAllowsMultipleSelection:YES];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[self members] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GreenTexturedCell *cell = nil;
Member *member = [self members][indexPath.row];
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"GreenTexturedCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]]) {
cell = (GreenTexturedCell*)view;
}
}
cell.textLabel.text = member.name;
cell.textLabel.font = [UIFont fontWithName:#"JandaCloserToFree" size:14.0];
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
Now I have another ViewController the MemberListViewController, it contains the MemberTableViewController (I know, the naming is not the best...). The MemberListViewController implements a protocol of the GroupSelectionTableViewController. Its delegate function will be called when the selected group changes. Here is the code for the MemberListViewController:
MemberListViewController.h
#interface MemberListViewController : UIViewController <GroupSelectionDelegate>
#property (weak, nonatomic) IBOutlet UIButton *groupSelection;
#property (nonatomic, strong) GroupSelectionTableViewController *groupTableView;
- (IBAction)groupSelectionPressed:(id)sender;
#end
MemberListViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self setGroupTableView:[GroupSelectionTableViewController new]];
[[self groupTableView] setDelegate:self];
}
- (IBAction)gruppeAuswahlPressed:(id)sender {
if ([self groupListShown]) {
[[[self groupTableView] view] removeFromSuperview];
[self setGroupListShown:NO];
} else {
[self.view addSubview:[[self groupTableView] view]];
[self setGroupListShown:YES];
}
}
- (void) groupSelectionController:(GroupSelectionTableViewController *)groupSelection DidSelectGroupWithId:(NSString *) groupId {
[[self groupSelection] setTitle:name forState:UIControlStateNormal];
[[[self groupTableView] view] removeFromSuperview];
[self setGroupListShown:NO];
MemberTableViewController *memberTableView = [self.storyboard instantiateViewControllerWithIdentifier:#"memberTable"];
[memberTableView setGroupId:[NSNumber numberWithInt:4017411]]; // Id for testing
[memberTableView refreshControl];
}
Now, if I change the group selection, the -groupSelectionController:DidSelectGroupWithId: Method is called and with [memberTableView refreshControl] I'm able to get into the viewDidLoad Method of the MemberTableViewController again and set the groupId to my testing id. Now i want the TableView to reload its content to get the members of the selected group. With the statement [self.tableView reloadData] I'm getting into the tableView:NumberOfRowsInSection: Method but not into the tableView:CellForRowAtIndexPath: Method and the TableView Content is not redrawn.
What am I doing wrong ?
This problem is driving me nuts, so I hope there's someone to help me out.
Thanks
With instantiateViewControllerWithIdentifier you are creating a new instance of MemberTableViewController, which isn't the one you are showing on screen. Hence, you won't see the changes which should be shown by calling reloadData.
To be able to reload the table data, you can add a link to the table at your MemberListViewController. Change the code of the .h to:
#import "MemberTableViewController.h"
#interface MemberListViewController : UIViewController <GroupSelectionDelegate>
{
IBOutlet MemberTableViewController *memberTableView;
}
#property (weak, nonatomic) IBOutlet UIButton *groupSelection;
#property (nonatomic, strong) GroupSelectionTableViewController *groupTableView;
- (IBAction)groupSelectionPressed:(id)sender;
#end
and then, in the .m, you can call [memberTableView reloadData];
Do not forget to set memberTableView as "new referencing outlet" within the storyboard file.