Detecting Reachability - iphone

I have Reachability class that I adopted from Apple. My problem is implementing my reachability detection in my ListViewController rather than in the ReachabilityAppDelegate shown in Apple. My problems:
I want to link the calling method in the (UITableViewCell *)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and the reachability detection
I am trying to disable my cell if they detect it is not connected and enable the cell if it
is connected
This is coded in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver: self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object: nil];
The reachabilityChanged as below:
-(void) reachabilityChanged: (NSNotification* )note{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability: curReach];
}
How do I implement my disabling of UITableViewCells in
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Take note that I have coded this in the above method:
NSInteger row = [indexPath row];
NSString *contentForThisRow = nil;
static NSString *MyIdentifier = #"MyIdentifier";
if (tableView == [[self searchDisplayController] searchResultsTableView]) {
// Sort search results in alphabetical order
NSArray *sorted = [searchResults sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
contentForThisRow = [sorted objectAtIndex:row];
}else {
contentForThisRow = [nameArray objectAtIndex:row];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]autorelease];
}
// Set Device names into Cells
cell.textLabel.text = contentForThisRow;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSLog(#"Load cell done");
}

you can code like this , add an instance var BOOL _isOffline
in the class and in your updateInterfaceWithReachability: method
- (void)updateInterfaceWithReachability:(Reachability* )curReach
{
if(curReach == XXXXNotReachable)
{
//your code
_isOffline = YES;
}
else
{
_isOffline = NO;
}
[_tableView reloadData];
}
in your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you should add your code to deal with the cell , may be
if(_isOffline)
{
cell.userInteractionEnabled = NO;
}
else
{
cell.userInteractionEnabled = YES;
}

Related

tableview as a intelligence in iphone not working

I have a textfield and a tableview. User may select textfield data from tableview or any other data.but after entering user own data my tableview must disappear.how can i achieve this.if i use resign first responder did select rowmethod is not working in tableview.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
txtcity.text = selectedCell.textLabel.text;
}
-(void)hideKeyboard //this is my gesture recogniser method
{
autocompletetableview.hidden=true;//if i didn't use this statement it enter into didselectrow method
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
autocompleteTableView.hidden=YES;
if( textField == txtcity)
{
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];// this method loads data into my tableview
return YES;
}
}
(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if([txtcity.text isEqualToString:#""])
{
autocompleteTableView.hidden=YES;
return YES;
}
else
autocompleteTableView.hidden=NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return arr2.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
}
cell.textLabel.text = [[arr2 objectAtIndex:indexPath.row]objectForKey:#"Name"];
cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
cell.textLabel.numberOfLines=0;
return cell;
}
if i use like this if i select row it doesn't enter into did select row at index path method. help me
i am not getting where i have to use resignfirstresponder
resignFirstResponder is used to dismiss the first responder ( keyboard in the case of UITextField). If you want the tableview to disappear set the hidden property to true or remove the tableview from the view hierarchy.
i.e;
[tableView setHidden:YES]
or
[tableView removeFromSuperview];
UPDATE:
If using gesture recognizer for checking on the tap on parent view, you can do the following so that the gesture method is not fired unnecessarily.
I'm assuming you are writing all this code in the view controller for the whole thing.
UITapGestureRecognizer *tapGe = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
tapGe.numberOfTapsRequired = 1;
tapGe.delegate =self;
[self.view addGestureRecognizer:tapGe]
Then implement the following method in the view controller (Make it conform to UIGestureRecognizerDelegate protocol):
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
If(touch.view==self.view){
return YES; //If its the main view accept the touch
}else{
return NO; //Otherwise(say tableview) don't consume the touch.
}
}
I think It's useful to you.I have used custom cell and I have give my working code here.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mainDetaileArray count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 115;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"ProductMerchantCell" owner:nil options:nil];
for (UIView *view in views)
{
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (ProductMerchantCell*)view;
//cell.img=#"date.png";
}
}
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
txtfield = [[UITextField alloc]initWithFrame:CGRectMake(104, 36, 58, 31)];
txtfield.textColor = [UIColor colorWithRed:56.0f/255.0f green:84.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
txtfield.delegate = self;
txtfield.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
txtfield.returnKeyType = UIReturnKeyDone;
indexVal = indexPath.row;
[txtfield setBorderStyle:UITextBorderStyleRoundedRect];
txtfield.textAlignment = UITextAlignmentCenter;
txtfield.text = [quantity_array objectAtIndex:indexPath.row];
// NSLog(#" txtfield.text %#", txtfield.text);
[txtfield setTag:indexPath.row];
[txtfield setAutocapitalizationType:UITextAutocapitalizationTypeWords];
NSString *total_str_price1=[[mainDetaileArray objectAtIndex:indexPath.row]valueForKey:#"total_product_price"];
cell.availbl_pro_lbl.text=[NSString stringWithFormat:#"Total : %#%#",add_currnce_str,total_str_price1];
cell.title_pro_lbl.text=[[mainDetaileArray objectAtIndex:indexPath.row]valueForKey:#"product_title"];
str_price=[[mainDetaileArray objectAtIndex:indexPath.row]valueForKey:#"product_price"];
cell.price_Pro_lbl.text=[NSString stringWithFormat:#"Price : %#%#",add_currnce_str,str_price];
[cell.contentView addSubview:txtfield];
return cell;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
addtoValue = textField.text;
[quantity_array replaceObjectAtIndex:textField.tag withObject:textField.text];
// NSLog(#"quantity_array %#",quantity_array);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
[cell.Quantity_txt resignFirstResponder];
return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[cell.Quantity_txt resignFirstResponder];
}

Multiple accessory checkmarks displayed on a UITableView

I have a UITableView that displays checkmarks when a row is selected. The problem is that when the table view scrolls multiple checkmarks are shown when only one was ever row selected. The problem arises as the table scrolls and the dequeue process occurs. Here's my cellForRowAtIndexPath: method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:56.0/255.0 blue:55.0/255.0 alpha:1];
}
// Get item from tableData
NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:#"key"];
[cell.textLabel setFont:[UIFont fontWithName: #"Asap-Bold" size: 14.0f]];
return cell;
}
and didSelect method:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
selectedCityTableString = cell.textLabel.text;
cellAccessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon-tick.png"]] ;
cellAccessoryNoneImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#""]] ;
if (cell.accessoryType==UITableViewCellAccessoryNone)
{
// cell.accessoryType=UITableViewCellAccessoryCheckmark;
cell.accessoryView = cellAccessoryImageView;
if (prev!=indexPath.row) {
cell=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:prev inSection:0]];
//cell.accessoryType=UITableViewCellAccessoryNone;
cell.accessoryView = cellAccessoryNoneImageView;
prev=indexPath.row;
}
}
else{
// cell.accessoryType=UITableViewCellAccessoryNone;
cell.accessoryView = cellAccessoryNoneImageView;
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *mutCityStr = [[selectedCityTableString stringByReplacingOccurrencesOfString:#" " withString:#"+"] lowercaseString];
// NSString *mutCityStr = #"c";
[prefs setObject:mutCityStr forKey:#"selectedCityTableString"];
[prefs synchronize];
#ifdef DEBUG
NSLog(#"mutCityStr is %#",mutCityStr);
NSLog(#"selectedCityTableString is %#",selectedCityTableString);
NSLog(#"posting notification from TWO TABLES");
#endif
[[NSNotificationCenter defaultCenter] postNotificationName:#"TTSelectedList" object:selectedCountryTableString];
}
Supposing you have a property (attribute) called selectedRow use the combination of this methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if (indexPath.row == self.selectedRow) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// If there is a cell selected deselect
if (self.selectedRow != NSNotFound) {
NSIndexPath *previousIndexPath = [NSIndexPath indexPathForRow:self.selectedAQIType inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:previousIndexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Select the touched cell
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedRow = indexPath.row;
}
In your .h file
int selectedRow;
In your .m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// your other code for cell init,etc
int row = [indexPath row];
cell.accessoryType = (row == selectedRow) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
cell.textLabel.textColor= (row == selectedRow) ? [UIColor colorWithRed:242.0f/255.0f green:104.0f/255.0f blue:42.0f/255.0f alpha:1] : [UIColor blackColor] ;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = [indexPath row];
[tableView reloadData];
}
Hope this helps!!!
UITableView's delegate method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath reuses the cell based on the reuseidentifier. Hence you need to reset the entire cell contents to default.
In your case uncheck the cell's view before you return the cell.
To solve this problem, store your selected row indexes in one NSArray. (in didSelectRowAtIndexPath method)
And in cellForRowAtIndexPath method, check whether indexPath.row is available in that array then enable your checkmark, else disable.
Note: Maintain array for check-uncheck event
Hope this will help you.

How UITableView show two different arrays one at a time?

The below Code works but not as i wish.i want that when i click UIbutton its automaically update the new value in UITableview instead of old value.Below Code works only when i press the UIbuttons and after that when i scroll the UITableview then it update the UItableview with new values.
In my application i using UITableview as Subclass of my mainclass.as image show below
I add Tableview in my Mainclass which is "testingViewController" like this way
In testingViewController.h
#import "Inputtableview.h"
#interface testingViewController :UIViewController<UITableViewDelegate,UITableViewDataSource> {
Inputtableview *inputview;
IBOutlet UITableView *inputtbl;
}
#end
In testingViewController.m
- (void)viewDidLoad {
btn1bool=FALSE;
if (inputview == nil) {
inputview = [[Inputtableview alloc] init];
}
[inputtbl setDataSource:inputview];
[inputtbl setDelegate:inputview];
inputview.view = inputview.tableView;
}
Now in Button action method
-(IBAction)input:(id)sender
{
btn1bool=TRUE;
}
my Subclass code "inputtableview.m" is show below
- (void)viewDidLoad {
[super viewDidLoad];
listOfItems=[[NSMutableArray alloc] initWithObjects:#"Iceland",#"Greenland",#"Switzerland",
#"Norway",#"New Zealand",#"Greece",#"Italy",#"Ireland",nil];
array1 = [[NSMutableArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H", nil] ;
}
#pragma mark -
#pragma mark Table View datasource methods
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (btn1bool) {
return [array1 count];
}
else {
return [listOfItems count];
}
[self.tableView reloadData];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(#"Row: %i", indexPath.row);
if (btn1bool) {
NSString *cellValue = [array1 objectAtIndex:indexPath.row];
cell.text = cellValue;
}
else {
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;
}
return cell;
}
Any help will be appriated.
Just put the following code:
[inputtbl reloadData];
There are a few things you need to change in your project, but I assume this project is just for testing stuff.
You want the date to reload after you pressed the button, so you call the method in the IBAction.
-(IBAction)input:(id)sender
{
btn1bool=TRUE;
[inputview.tableView reloadData];
}
To switch between the 2 data sources when the button is pressed you can change to this line of code: btn1bool=!btn1bool;
(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (btn1bool) {
return [array1 count];
} else {
return [listOfItems count];
}
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is correct

Calling UITable View Delegate Method

I'm working with UITableView in UIViewController..I am using the following delegate methods.....
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Once I load the view I find all delegates has been called..... But I need to call these methods in a button click.....
I tried in this way....
[self tableView:table numberOfRowsInSection:rows];
[self tableView:table cellForRowAtIndexPath:path];
I find that these methods has been called,but nothing happened (code inside these delegates doesn't works).... I don't knw why??? Any suggestions???
Write the code inside the button click function it will call all the delegates of the tableview
[self.tableView reloadData];
or complete code to your solution is:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
customCell *cell = [[customCell alloc]init];
if (cell == nil) {
cell = [[[customCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:#"customCell" owner:self options:nil];
cell = (customCell *)[topLevelObjects objectAtIndex:0];
int count=0;
for(id currentObject in topLevelObjects)
{
count++;
if([currentObject isKindOfClass:[customCell class]])
{
cell= (customCell *)currentObject;
UILabel *sName = (UILabel *)[cell.contentView viewWithTag:1];
//Checking the flag that is set in the btnClicked event
if (flag) {
sName.hidden=YES;
}
else{
sName.text = [arrayResult objectAtIndex:indexPath.row];
}
}
}
return cell;
[topLevelObjects release];
[cell release];
}
-(IBAction) btnClicked:(id) sender{
flag=YES;
[self.tableView reloadData];
}
Try this it will work..

Searching a TableView of annotations

I had no idea how to set up a database when i started so i have 80 annotations that all look like this
workingCoordinate.latitude = 50.795825;
workingCoordinate.longitude = -1.103139;
MainViewAnnotation *Levant = [[MainViewAnnotation alloc] initWithCoordinate:workingCoordinate];
[Levant setTitle:#"Levant"];
[Levant setSubtitle:#"Unit R09, Gunwharf Quays"];
[Levant setAnnotationType:MainViewAnnotationTypePortsmouth];
[mapView addAnnotation:Levant];
They are grouped into 22 cities through the MainViewAnnotationType, which is coded as follows:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 21;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == MainViewAnnotationTypeBirmingham)
{
return #"Birmingham";
}
else if(section == MainViewAnnotationTypePortsmouth)
{
return #"Portsmouth";
}
return nil;
}
The annotations are then put into a TableView like so;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSMutableArray *annotations = [[NSMutableArray alloc] init];
if(indexPath.section == 0)
{
for(MainViewAnnotation *annotation in [mapView annotations])
{
if(![annotation isKindOfClass:[MKUserLocation class]])
{
if([annotation annotationType] == MainViewAnnotationTypeBirmingham)
{
[annotations addObject:annotation];
}
}
}
cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
}
...
else if(indexPath.section == 17)
{
for(MainViewAnnotation *annotation in [mapView annotations])
{
if(![annotation isKindOfClass:[MKUserLocation class]])
{
if([annotation annotationType] == MainViewAnnotationTypePortsmouth)
{
[annotations addObject:annotation];
}
}
}
cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
}
return cell;
}
and then when a cell is clicked, will zoom in on the annotation.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for(MainViewAnnotation *annotation in [mapView annotations])
{
if([[[[tableView cellForRowAtIndexPath:indexPath] textLabel] text] isEqualToString:[annotation title]])
{
[mapView setRegion:MKCoordinateRegionMake([annotation coordinate], MKCoordinateSpanMake(.003, .003)) animated:YES];
}
}
}
My question is, I would like to be able to search my annotations either through their city their in or by their annotation name. I have a search bar controller already in my interface and it has been connected to my tableview, just not written any code.
I would like the search to be hidden unless i click a search button then hide when i click on a cell.
New to Xcode, any help including some code examples would be much appreciated.
What I did is to create my UITableView not programmatically but using Interface Builder. Create your own custom ViewController and add inside an UITableView. Don't forget to set (by interface builder) delegate and datasource. Afterwards I think it might be easier for you to manage your hidden/show search bar.
Hope this can help.