Unselecting the table column when ever Sorting happens - objective-c

I have an Application which loads an Table on click of an button, currently for the table Sorting option has been removed, But when ever I click on any column and then Click on Table Header , the selected Column should get de-selected. Not sure how to achieve this.
Thanks in Advance!!!!

You have to trigger 'buttonSelected:' method :)
#interface ViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSIndexPath *currentSelectedIndexPath;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation ViewController
#pragma mark - UIViewControllerDelegate
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
self.currentSelectedIndexPath = nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.currentSelectedIndexPath = indexPath;
}
- (IBAction)buttonSelected:(UIButton *)sender {
[self.tableView deselectRowAtIndexPath:self.currentSelectedIndexPath animated:YES];
}
#end

Related

How to programmatically create a UITableView in UIPopover so it responds to -cellForRowAtIndexPath

I have 3 UITableViews in a class; one of the tableViews is programmatically created in a UIPopover where I assign it a tag. In -cellForRowAtIndexPath, I check the tag each tableView and configure the tableView depending on the tag id.
The problem is the popover is not created until after -cellForRowAtIndexPath is called. I don't see how I can have a separate -cellForRowAtIndexPath in the method that creates the tableView in the popover. I have the tableView.dataSource = self in the method that creates the tableView.
How can I point the tableView in the popover to it's own -cellForRowAtIndexPath?
So it wasn't entirely clear, but it seems as though your asking how to assign different cellForRowAtIndexPath for the different TableView's in your class. To this end, I've created this small piece of sample code to illustrate how you could accomplish having differing sets of data sources for multiple UITableView's in a single class.
As you can see, there are three different DataSource objects that can each independently control the cellForRowAtIndexPath for each of the three different UITableView's. There's no reason you can't have two table views utilize a single DataSource, and then the third table use it's own.
*Note: There is no reason to keep all of this in a single file, but if that is your desire you certainly can do that.
//UITableViewOne
#interface DataSourceOne : NSObject <UITableViewDataSource>
#end
#implementation DataSourceOne
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell for TableViewOne
}
#end
//UITableViewTwo
#interface DataSourceTwo : NSObject <UITableViewDataSource>
#end
#implementation DataSourceTwo
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell for TableViewTwo
}
#end
//UITableViewThree
#interface DataSourceThree : NSObject <UITableViewDataSource>
#end
#implementation DataSourceThree
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell for TableViewThree
}
#end
#interface MultiTableViewController ()
#property (nonatomic,strong) UITableView *tableOne;
#property (nonatomic,strong) UITableView *tableTwo;
#property (nonatomic,strong) UITableView *tableThree;
#property (nonatomic,strong) DataSourceOne *sourceOne;
#property (nonatomic,strong) DataSourceTwo *sourceTwo;
#property (nonatomic,strong) DataSourceThree *sourceThree;
#end
#implementation MultiTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sourceOne = [DataSourceOne new];
self.sourceTwo = [DataSourceTwo new];
self.sourceThree = [DataSourceThree new];
//Create or Load TableViews from Xib
self.tableOne.dataSource = self.sourceOne;
self.tableTwo.dataSource = self.sourceTwo;
self.tableThree.dataSource = self.sourceThree;
}
Let me know if you want any more clarification, or if you have further questions on the topic.

Delegate method not being called by viewController

So here's what's going on (other than me being a noob).
I have two view controllers, AddAPlaceViewController and showCategoriesViewController.
I have a table cell in AddAPlaceVC which you tap and showCategoriesVC (which is a tableviewcontroller) opens up (modal) and gives you a list of ten options. You tap one of them or tap on the cancel button.
I've spent days understanding and figuring out protocols and delegates. I think I have them set up right but can't figure out why they aren't working or getting called. I've tried all sorts of changes but no luck so far.
You guys are my only hope! :)
Here's the important bits of the code:
AddAPlaceViewController.h
AddAPlaceViewController.h
#import <UIKit/UIKit.h>
#import "showCategoriesViewController.h"
#interface AddAPlaceViewController : UIViewController <UITextViewDelegate, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, UITableViewDataSource, UITableViewDelegate, ShowCategoriesViewControllerDelegate>
AddAPlaceViewController.m
AddAPlaceViewController.m
#import "AddAPlaceViewController.h"
#import "FSQVenue.h"
#import "Categories.h"
//#import "showCategoriesViewController.h"
#import <QuartzCore/QuartzCore.h>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Un-highlight the selected cell
[categoryTable deselectRowAtIndexPath:indexPath animated:YES];
showCategoriesViewController *SCVC = [[showCategoriesViewController alloc] init];
SCVC = [self.storyboard instantiateViewControllerWithIdentifier:#"showCategories"];
SCVC.categoryDelegate = self;
[self.navigationController presentModalViewController:SCVC animated:YES];
}
#pragma mark showCategoriesViewControllerDelegate
-(void)didSelectOptions:(NSInteger )selectedCategory
{
NSInteger category = selectedCategory;
NSLog(#"Add Places %d", category);
[self dismissModalViewControllerAnimated:YES];
}
-(void)didCancelOptions
{
NSLog(#"Add Places -- Dismiss Button Pressed");
[self dismissModalViewControllerAnimated:YES];
}
showCategoriesViewController.h
showCategoriesViewController.h
#import <UIKit/UIKit.h>
#protocol ShowCategoriesViewControllerDelegate;
#interface showCategoriesViewController : UITableViewController
{
id<ShowCategoriesViewControllerDelegate>categoryDelegate;
}
#property (nonatomic, weak) id<ShowCategoriesViewControllerDelegate> categoryDelegate;
- (IBAction)cancelButton:(id)sender;
#end
#protocol ShowCategoriesViewControllerDelegate <NSObject>
- (void)didSelectOptions:(NSInteger )selectedCategory;
- (void)didCancelOptions;
#end
showCategoriesViewController.m
showCategoriesViewController.m
#import "showCategoriesViewController.h"
#import "Categories.h"
#interface showCategoriesViewController ()
#property (strong, nonatomic) NSArray *categoryArray;
#end
#implementation showCategoriesViewController
#synthesize categoryDelegate = _categoryDelegate;
#synthesize categoryArray;
…
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[_categoryDelegate didSelectOptions:indexPath.row];
NSLog(#"Selected IndeXPath %#", cell);
NSLog(#"Selected IndeXPath GOING TO DELEGATE %d", indexPath.row);
}
- (IBAction)cancelButton:(id)sender {
[_categoryDelegate didCancelOptions];
NSLog(#"Button Pressed");
}
Most likely guess? In the code you posted, both your property and your ivar are called categoryDelegate, yet you treat your property as though it refers to an ivar called _categoryDelegate. Probably this is just a typo, but because iOS allows calls to methods on null objects (and just doesn't do anything), it fails silently. Renaming the ivar to _categoryDelegate should fix it.
Since you have presentModalViewController: your SCVC, your AddAPlaceViewController is inactive. Its better if you can create your showCategoriesViewController as a subclass of UITableView with your custom delegate method.
Reference Link

UITableViewCell does not appear in my table

I have an UITableVIew. Now I made my own UITableViewCell. But if my table appears nothing is shown. However the cells are still selectable and open the DetailView. Which command have I forgotten?
//Customize the appearance of table view cells.
-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MainTableCell";
MainTableCell *cell = (MainTableCell*)[tableView dequeueReusableCellWithIdentifier:Cel lIdentifier];
if (cell == nil) {
cell = [[[MainTableCell alloc] initWithStyle:UITableViewCellStyleDef ault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
VerwaltungInformation *selectedFormel = [listOfFormularies objectAtIndex:indexPath.row];
cell.FormelNameLabel.text = selectedFormel.nameFormel;
return cell;
}
Do I have to add special things? If somebody needs more code - please tell me.
Here is my MainTableCell.h:
import <UIKit/UIKit.h>
#interface MainTableCell : UITableViewCell {
UILabel *FormelNameLabel;
UILabel *ErklaerungVerfuegbarLabel;
UILabel *BeispielVerfuegbarLabel;
UILabel *RechnerVerfuegbarLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *FormelNameLabel;
#property (nonatomic, retain) IBOutlet UILabel *ErklaerungVerfuegbarLabel;
#property (nonatomic, retain) IBOutlet UILabel *BeispielVerfuegbarLabel;
#property (nonatomic, retain) IBOutlet UILabel *RechnerVerfuegbarLabel;`
#end
And here is my MainTableCell.m:
#import "MainTableCell.h"
#implementation MainTableCell
#synthesize FormelNameLabel;`
#synthesize ErklaerungVerfuegbarLabel;
#synthesize BeispielVerfuegbarLabel;
#synthesize RechnerVerfuegbarLabel;`
- (void)dealloc {
[FormelNameLabel release];
[ErklaerungVerfuegbarLabel release];
[BeispielVerfuegbarLabel release];
[RechnerVerfuegbarLabel release];
[super dealloc];
}
#end
you have commented this line
cell. VerwaltungInformation *selectedFormel = [listOfFormularies objectAtIndex:indexPath.row];
and also check wthr
selectedFormel.nameFormel
contains any value.
ok, I'll try to answer.
1.try to NSLog this value selectedFormel.nameFormel before setting it to the label.text.
NSLog(#"selectedFormel.nameFormel:%#", selectedFormel.nameFormel);
2.Have you configured your cell properly? are you sure that for example FormelNameLabel's frame is not zero?
NSLog(#"cellFrame:%#", NSStringFromCGRect(cell.frame));
Are you sure that you have added a label to the cell as subview?
If all these things are done and it still shows nothing provide a code from your cell!

How to reload UItableView that is inside UiViewController

I have a UITableView inside UiViewController.
In the same screen, I also have two buttons: Show All vs Show Top 5.
Now based on a selection all/top 5, I have to update table data.
I cant use [tableView reloadData] as I am not using UITableViewController.
This is the first time I am working on an iphone app. So any help is appreciated.
(I used this tutorial to get started http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/)
Thanks.
Here is a snippet of my code:
.h file
#interface DataViewController : UIViewController {
NSString *showType;
}
#property (nonatomic, retain) NSString *showType;
-(IBAction) showTop: (id) sender;
-(IBAction) showAll: (id) sender;
#end
.m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"customCell";
DataCustomCell *cell = (DataCustomCell *) [tableView dequeueReusableCellWithIdentifier:cellID];
if([showType isEqualToString:#"all"])
{
// use this data..
}
else
{
// use some other data..
}
// ....
}
-(IBAction) showNearby: (id) sender
{
self.showType=#"top";
// reload table some way
}
-(IBAction) showAll: (id) sender
{
self.showType=#"all";
//reload table some way
}
Create a UITableView IBOutlet like this
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
in your UIViewController's interface file. Then have that connect to the UITableView in Interface Builder. After synthesizing it in your implementation file, you should be able to access it like this
[self.myTableView reloadData];
Also, since you retained it, you will have to release myTableView in the dealloc method.

Why isn't my custom delegate working?

I'm trying to build a delegate to allow me to pass data form a child view controller to the parent. I've been looking at various tutorials / questions online but my delegate method isn't being triggered.
Could you take a look at my code below and see if I'm missing anything?
TownListViewController.h
#protocol TownListViewControllerDelegate;
#interface TownListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
id <TownListViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <TownListViewControllerDelegate> delegate;
#end
#protocol TownListViewControllerDelegate
#optional
- (void)didSelectTown:(Town *)town;
#end
TownListViewController.m
#implementation TownListViewController
#synthesize delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[delegate didSelectTown:(Town *)[self.fetchedResultsController objectAtIndexPath:indexPath]];
[self.navigationController popViewControllerAnimated:YES];
}
#end
SearchViewController.h
#interface SearchViewController : UITableViewController <TownListViewControllerDelegate> {
...
}
SearchViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TownListViewController * townViewController = [[TownListViewController alloc] initWithNibName:#"TownListViewController" bundle:nil];
townViewController.delegate = self;
[self.navigationController pushViewController:townViewController animated:YES];
}
- (void)didSelectTown:(Town *)town
{
NSLog(#"didSelectTown fired");
self.selectedTown = town;
}
Any help is much appreciated.
try to reloadData after set the delegate. I think the first call to reloadData of table occurs during the initWithNibName of the TownListViewController... Anyway can you post the delegate value on this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I bet is nil...
By the way is a good practice to check if the variable delegate conforms the protocol, try adding this line under supposing that the method is optional:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([delegate conformsToProtocol(TownListViewControllerDelegate)] && [delegate respondsToSelector:#selector(didSelectTown:objectAtIndexPath:)])
[delegate didSelectTown:(Town *)[self.fetchedResultsController objectAtIndexPath:indexPath]];
[self.navigationController popViewControllerAnimated:YES];
}