How to refresh ViewController code? - iphone

I have a view controller with a table with custom row (image and text).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"CustomRow";
CustomRow *cell = (CustomRow *)[_default dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomRow" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
x = x + 1;
trovato = [util cerca:giorno :[NSString stringWithFormat:#"%d", x]];
if ([trovato isEqualToString:#"NO"]) {
cell.pallino.image = [UIImage imageNamed:#"grigio.png"];
} else {
cell.pallino.image = [UIImage imageNamed:#"verde.png"];
}
cell.title.text = [right objectAtIndex:indexPath.row];
return cell;
}
This work correct the first time I open the View controller, when I tap on a row I have to open a another view controller and I do some changes. When I come back with Navigation bar I want that the table refresh again the image (the most important things), but I cannot do this. What I have to do????
Thanks

You have to refresh your table view (reloadData) when you come back from you sub view, e.g. in your viewWillAppear function.

When you tap on row save indexpath for that row
You make chanhes in detailviewcontroler (your next view controller which open after row press)
when you come back check
if there are change then reloadRowsAtIndexPaths: method to reaload that cell else do nothing (as you did not do any changes)
To check if you have made any changes on detailviewcontroler or not you can use delegation or with simplest approach reload selected row always when you cam back from detailviewcontroler.
you can refresh cell in
-(void) viewWillAppear:(BOOL)animated by using saved indexpath

You can use following ways,it just use it on -(void)viewDidAppear:(BOOL)animated
[self.tableView reloadData];
or
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
hope this will work

Related

webview is not loading from table view cell

Im adding a custom cell. Whenever someone presses the add button, a alert view pops up and they add a name of a website. once pressed saved, it gets saved in the table view cell under whatever name they choose. Then once someone clicks that cell i want it to load with http:// in my web view. the only problem is that its not loading. What am I doing wrong?
This is my table view controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cellTwo";
UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if (indexPath.section == 0) {
cell.textLabel.text = [NSString stringWithFormat:#"%#",[tableData objectAtIndex:indexPath.row]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Index Selected,%d",indexPath.row);
MainTwoViewController *View = [self.storyboard instantiateViewControllerWithIdentifier:#"mainTwo"];
View.urlString = [[NSString alloc] initWithFormat:#"http://www.%#",tableData];
View.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//Only do the following action if the user hits the ok button
if (buttonIndex == 1) {
NSString *tapTextField = [alertView textFieldAtIndex:0].text;
if (!tableData) {
tableData = [[NSMutableArray alloc]init];
}
[tableData insertObject:tapTextField atIndex:0];
[myTableView reloadData];
}
and of course i put this in the mainTwo view controller so it loads the urlString.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
You instantiate a view controller but do not show it.
Use storyboard segues instead and configure the new view controller in prepareForSegue.
Also, check the following:
If you are using storyboard, most likely you do not have to create cells in cellForRowAtIndexPath.
Also, simplify:
[NSString stringWithFormat#"%", string];
--> string
Finally, check the url that arrives at the new view controller before you load the request. Make sure sure the url is correct. Implement the UIWebViewDelegate methods to check for loading failures or unexpected return data.
i think your this code
View.urlString = [[NSString alloc] initWithFormat:#"http://www.%#",tableData];
should be like
View.urlString = [[NSString alloc] initWithFormat:#"http://www.%#",[tableData objectAtIndex:indexPath.row]];
at your didSelectRowAtIndexPath method, and it is also advisable that you use NSLog to check at which point you loose your data
Ok, I just identified your problem.
What you should do is put a tap gesture on the custom cell label first of all.
UITapGestureRecognizer *openURLTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openURLTapped:)];
keypadDisableTapGestureRecognizer.numberOfTapsRequired = 1;
keypadDisableTapGestureRecognizer.numberOfTouchesRequired = 1;
[cell.textLabel addGestureRecognizer:openURLTapGestureRecognizer];
Now, in this openURLTapped, you can pass the url and open it.
-(void)openURLTapped:(UITapGestureRecognizer*)tap
{
// your code here
}
It will definitely resolve your issue.

iOS - UITableView Changing Datasourse

I was wondering if anyone knows how to solve this problem:
I have a UITableview, where the datasource can continue to grow. The issue is when I add elements to the data source, the table cells get messed up.
Each table cell has a text field where the user can enter data into it, but whenever I add data to the datasource the comments replicate to other cells.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"ImageTableCell";
ImageTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Picture *aPicture = (Picture *) [self.imageData objectAtIndex:[indexPath row]];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ImageTableCell" owner:self options:NULL];
cell = (ImageTableCell *)[nib objectAtIndex:0];
}
NSLog(#"comment %# index %d", aPicture.comment, [indexPath row]);
cell.cellImage.image = aPicture.picture;
cell.commentField.delegate = self;
cell.commentField.text = aPicture.comment;
cell.index = [indexPath row];
cell.tag = [indexPath row];
return cell;
}
self.imagedata is a NSMutabaleArray.
EDIT
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.imageData count];
}
-(void) reloadImages:(NSNotification *) aNotification {
self.imageData = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).currentUserData.images;
[self.imageTableView reloadData];
}
a screenshot http://www.mikerizzello.com/pic.png
Thanks
EDIT:
I think you might have a bad MVC pattern. Here's what I would do:
Take your custom cell, ImageViewCell, and make that class your textField delegate. Add a property for your custom Picture object, and set that in cellForRow...
Then, in your textFieldDelegate methods inside the custom cell class, you can change set the contents of your PictureObject there. I have a feeling that you're handling text input in the viewController, and the index of the data object in your array is getting mixed up and you're applying the comment field text to the wrong item in the array.
EDIT
I think it has something to do with cell reuse. The contents of your cell are not getting wiped out upon reusing a cell.
Try this block before you return the cell:
if (!aPicture.comment) {
cell.commentField.text = #"";
} else {
cell.commentField.text = aPicture.comment;
}
Have you try with
if (cell == nil) {
cell = (ImageTableCell*)[[ImageTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Anyway you can also set different reuseIdentifier for each of cells but it will be bad idea, I would like to help you but i think need to run this project.
Please let me know if code above give better solution.

TableView adding a cell

I'm doing a project in which I have a TableView in which I have different Rows/Cell and in each cell there is a add button. What I need to do is on clicking the add button the new row should add below the row in which I clicked the button, means I have to add a cell to the next of the selected button on the row.and also a user can enter a text on the new row. Please tell me how to do this . As I'm new in iPhone development. I will be very thankful ....
Here is a click button function in which I want to perform an action to add new cell.
- (IBAction)AddButtonAction:(id)sender
{
[_choices insertObject:#"avav" atIndex:[_choices count]];
[self.tableView reloadData];
NSLog(#"here");
}
Use this delegate method to add a new row below the selected row. And use custom cell to have a text field on it...
rowCount ++;//Here rowcount refers the no. of rows in the table.
selectedIndexPath = indexPath;//Assign the selected indexpath for creating custom cell on it.
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];//Indexpath refers the currently selected/targeted cell.
In cellforRowAtIndexPath use like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if((selectedIndexPath) && (indexPath.row == selectedIndexPath.row) && (indexPath.section == selectedIndexPath.section))
{
static NSString *cellIdentifier=#"cell";
NewCell *cell = (NewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSString *customeCellName = [NSString stringWithFormat:#"%#",[NewCell class]];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (NewCell *) currentObject;
break;
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
else
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
// Configure the cell...
return cell;
}
}
OutPut will be like this
need to customize as per your requirement.
I consider you have textfeild in all cells. In AddButtonAction function save index of row. While creating cells make selected cell textfeild firstresponder. Once user enter data save it in array and again reload. Just keep track of selectedIndex properly.
Ifeel this will help.
If you add button to cell by addSubview method, simply you can get indexPath with the following code:
- (IBAction)AddButtonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)[button superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Adding methods comes here...
}
If you are using some addSubview levels, you should use same superview methods inverse to access the UITableViewCell level. If using custom cells, replace your custom cell classname with UITableViewCell in the above code.
For adding cells, take a look at WWDC 2011 Videos, "UITableView Changes, Tips & Tricks" video. It shows some nice and useful methods to insert, delete and reload new rows/sections between other rows/sections like insertRowsAtIndexPaths:withRowAnimation: method. Or see Apple Documents.

IOS UItableview reloadRowsAtIndexPaths refresh issue

I want to update to tableview custom cell. I want to refresh table cell When I search. I use reloadRowsAtIndexPaths methods. This method work, But did not update cell. Can you help me please
Below method run When I searched
-(void)doSearchHotelName:(id)sender{
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:1 inSection:0];
[self.tableV beginUpdates];
[self.tableV reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationFade];
[self.tableV endUpdates];
}
Below method table method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Hotel_FilterSearchCell";
Hotel_FilterSearchCell_iPad *cell = (Hotel_FilterSearchCell_iPad *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib ;
nib = [[NSBundle mainBundle] loadNibNamed:#"Hotel_FilterSearchCell_iPad" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
else if ([indexPath row]==1) {
if([SharedAppDelegate.filter_selected_hotels_list count]==[SharedAppDelegate.filter_hotels_list count])
[cell.cellExtraInfo setText:#"All(H)"];
else if ([SharedAppDelegate.filter_selected_hotels_list count]!=[SharedAppDelegate.filter_hotels_list count])
[cell.cellExtraInfo setText:[NSString stringWithFormat:#"%i %#",[SharedAppDelegate.filter_selected_hotels_list count],#"Selected(H)"]];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
Try changing UITableViewRowAnimationFade to UITableViewRowAnimationNone and let me know if that helps.
I just had to do the same because I'm using a static UITableView in my storyboard and it seems any animation moves the old cell out of view and replaces with the new cell. This basically removes the cell from the table because the old cell and the new cell are the same object(my assumption).
From UITableView documentation:
beginUpdates
Begin a series of method calls that insert, delete, or select rows and sections of the receiver.
You should not use this method unless you are inserting, deleting or selecting a row or a section.

uitableView reloadData doesn't work after setting delegate, datasource and file's owner connection

I have googled and done lot of research from my side to find out why the reloadData method on tableview wouldn't work. I checked all the possible solutions like the datasource is set, delegate is set, the tableview is connected to the file's owner.
After all these, when I am trying to reload the tableview, the no. of rows method gets called, but the cell for rowAtIndexPath doesn't get called. Below is the code that I have written. Please let me know, where I am going wrong
- (void)onReservationListSuccess:(NSArray *)rData
{
if ( rData != nil )
{
resList = [[NSArray alloc] initWithArray:rData];
if([resList count] > 0)
{
[self.tripsTableView reloadData];
//[self.tripsTableView beginUpdates];
//[self.tripsTableView reloadSections:[NSIndexSet indexSetWithIndex:0]
// withRowAnimation:UITableViewRowAnimationNone];
//[self.tripsTableView endUpdates];
}
else
{
[tripsTableView reloadData];
[tripsTableView setHidden:YES];
[noTripsLabel setHidden:NO];
}
}
if(fsnNeedsRefresh == YES)
{
[[NSNotificationCenter defaultCenter] postNotificationName:UpdateFSNList object:nil];
fsnNeedsRefresh = NO;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int temp=[resList count];
NSLog(#"The no. of rows are %d", temp);
NSLog(#"Testing Purpose");
NSLog(#"The pnr details of the object is:%#",((TripData *)[resList objectAtIndex:0]).pnrDescription);
return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"The cell for the row at indexpath is getting called");
static NSString *CellIdentifier = #"TripCellIdentifier";
TripCell *cell = (TripCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TripCell" owner:self options:nil];
for(id oneObject in nib)
if([oneObject isKindOfClass:[TripCell class]])
cell = (TripCell *)oneObject;
}
// Set up the cell...
TripData *tripData = (TripData *)[resList objectAtIndex:indexPath.row];
cell.pnrLabel.text = tripData.pnr;
NSLog(#"The cell text is %#",tripData.pnr);
cell.pnrDescriptionLabel.text = tripData.pnrDescription;
NSLog(#"The cell text is %#",tripData.pnrDescription);
cell.pnrTypeLabel.text = tripData.pnrType;
NSLog(#"The cell text is %#",tripData.pnrType);
if(checkInAllowed)
{
cell.checkInButton.tag = indexPath.row;
[cell.checkInButton addTarget:self action:#selector(checkIn:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
[cell.checkInButton setEnabled:NO];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller
TripData *tripData = (TripData *)[resList objectAtIndex:indexPath.row];
NSLog(#"%#", tripData.pnr);
if(tripData != nil)
{
TripOverviewViewController *tripOverviewViewController = [[TripOverviewViewController alloc] initWithTrip:tripData];
[self.navigationController pushViewController:tripOverviewViewController animated:YES];
[tripOverviewViewController release];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
From this part of code I cannot say exactly why it does not work but I'll try to explain how reloadData works.
First, how UITableView works: basically, it's a scrollview. When it is drawn, it checks how many rows it has, then checks their height and from its size and scroll position it decides which rows are currently displayed. Then it asks the delegate to return a UITableViewCell for every displayed row.
When the table is scrolled, it removes the hidden cells from the view hierarchy and adds the cells that have appeared.
And now the tricky part - what does reloadData do? It just removes all the UITableViewCells from the table hierarchy. Nothing more. The actual update is done when the table is drawn for the first time after reloadData.
So, my suggestion is - check that your table is not hidden and check its frame. Also, I see that you are accessing both a property getter self.tripsTableView and an ivar tripsTableView. This is confusing. Do they both return the same?