I have a menu with two items, Login and Settings, displayed in a tableView. Login and Settings are stored in an NSArray. On ViewDidLoad I am checking if user exist in NSUserDefaults. If user exist then change the Login to Logout and vice versa. The issue is that on ViewDidLoad it only changes the name once when the application is opened after being cleared from cache. Therefore, in order to see the change after login in or login out you need to clear the application from cache and re-open it. I want the changes to apply immediately. Every time the menu button click before the menu open and viewDidAppear is accessed. I know this because I placed a break point and traced it. It does what is supposed to. But the it never changes the value of login to logout or vice versa.
MenuController.
-(void)ChangeLoginLabel {
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *User = [data objectForKey:#"User"];
if(User==nil) {
_extraMenuItems = [[NSMutableArray alloc] initWithObjects:#"Settings", #"Login", nil];
}
else {
_extraMenuItems = [[NSMutableArray alloc] initWithObjects:#"Settings", #"Logout", nil];
}
}
- (void)viewWillAppear:(BOOL)animated {
[self ChangeLoginLabel];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self ChangeLoginLabel];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.extraMenuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Formal";
UITableViewCell *cell = [self.extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *cellValue = [self.extraMenuItems objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor blackColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
NSLog(#"Settings cell tapped!");
}
else if (indexPath.row == 1)
{
//closes when login view appears
[self.slidingViewController resetTopView];
NSString *LoginTextLabel = [self.extraMenuItems objectAtIndex:indexPath.row];
if([LoginTextLabel isEqualToString:#"Logout"]) {
}
else {
UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Login"];
[self presentViewController:vc animated:YES completion:nil];
}
}
// Deselect the row
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Logs
2014-12-12 13:06:53.290 xxx[1695:270056] _extraMenuItems: (
Settings,
Login
)
2014-12-12 13:07:04.290 xxx[1695:270056] _extraMenuItems: (
Settings,
Logout
)
How can I force the array to change the value immediately when the user logged in or logged out?
If I'm understanding correctly, you're trying to update the label in your table based on the change in NSUserDefaults as triggered by a login/logout from another view controller; so when you return to the MenuViewController, ChangeLoginLabel is triggered by the viewDidAppear, the extraMenuItems array is in fact set to contain the appropriate values, and you expect the labels in the table to change accordingly. But the problem is, that you're not reloading the table upon your return to the view.
So first off, I'd recommend removing [self ChangeLoginLabel]; from your viewDidLoad since it's redundant. And secondly, I'd recommend changing your ChangeLoginLabel method to include a table reload, like so:
-(void)ChangeLoginLabel {
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *User = [data objectForKey:#"User"];
if(User==nil) {
self.extraMenuItems = [[NSMutableArray alloc] initWithObjects:#"Settings", #"Login", nil];
}
else {
self.extraMenuItems = [[NSMutableArray alloc] initWithObjects:#"Settings", #"Logout", nil];
}
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated {
[self ChangeLoginLabel];
}
Or if you'd prefer just to reload the relevant row you can replace [self.tableView reloadData]; with:
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
Do you want viewWillAppear instead? viewDidAppear happens after the view already is rendered I believe.
call
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self ChangeLoginLabel];
}
or
- (void)viewDidAppear:(BOOL)animated {
[super viewDifAppear:animated];
[self ChangeLoginLabel];
}
Related
I have a small issue in Updating the details in the view. How to reload a view after successful updation to change the details.
Below is my code. Please help me to find out the solution. Thanks in Advance.
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated
{
[self viewDidLoad];
[super viewWillAppear:animated];
[self.view setNeedsDisplay];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
profilearray=[[NSMutableArray alloc]initWithObjects:#"First Name:",#"Last Name:",#"Date of Birth:",#"Email:",#"Gender:",#"Address:",#"Country:",#"State:",#"City:",#"ZipCode:",#"Phone:",#"Guardian / Caretaker Details:",#"Name:",#"Relationship:",#"Email:",#"Doctor Details:",#"Name:",#"Phone:",#"Email:",#"Insurance Details:",#"Name:",#"Email:",nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [profilearray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [profilearray objectAtIndex:indexPath.row];
if(indexPath.row==0)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"FirstName"];
}
if(indexPath.row==1)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"LastName"];
}
if(indexPath.row==2)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"DOB"];
}
if(indexPath.row==3)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"Email"];
}
if(indexPath.row==4)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"Gender"];
}
if(indexPath.row==5)
{
cell.detailTextLabel.text=[[NSUserDefaults standardUserDefaults] stringForKey:#"Address"];
}
return cell;
}
use [tableView reloadData] for updating tableview
Simple Use this Do not need to view refresh or reload
-(void)viewWillAppear:(BOOL)animated
{
[tableView reloadData]
[super viewWillAppear:animated];
}
Your data source array profilearray keeps getting re-assigned the same value every time the view appears. Maybe initialise it once in viewDidLoad.
After the array is updated, use [tableView reloadData] to reload the table view.
Do not call viewDidLoad on your own, and get rid of setNeedsDisplay for now.
Just use this.
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
profilearray=[[NSMutableArray alloc]initWithObjects:#"First Name:",#"Last Name:",#"Date of Birth:",#"Email:",#"Gender:",#"Address:",#"Country:",#"State:",#"City:",#"ZipCode:",#"Phone:",#"Guardian / Caretaker Details:",#"Name:",#"Relationship:",#"Email:",#"Doctor Details:",#"Name:",#"Phone:",#"Email:",#"Insurance Details:",#"Name:",#"Email:",nil];
[tableView reloadData];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
profilearray:- Perhaps this is your array from where you are getting data in tableview and also storing data in it
viewWillAppear :- this method is called when view is appear
[tableView_Outlet reloadData] :- this method reload your tableview with new data
So in this scenario never call viewWillAper Because this is not standard way to do
Find point where your data is updated then call "reloadData"
For more about viewWillApear hit
Thanks
I've read every post and tried every solution multiple times, but cannot get correct functionality.
I have a tableview with data from a local JSON file. I need the user to be able to:
select multiple cells
show check marks on selected cells
write those selections to an array
delete the selections when unchecked
5. save/retain the selections with the check marks when user switches view or leaves and comes back to tableview, closes and reopens app, etc.
I've managed to get 1-4 working, but I'm stuck on #5 and can't figure it out for the life of me. I've tried NSUserDefaults every way I could. Any help is appreciated. Below is the current code.
Also, why am I having to double click a cell to uncheck it?
#interface FilterViewController () <UISearchResultsUpdating>
#property (nonatomic, strong) NSArray *IngredientsArray;
#property (nonatomic, strong) UISearchController *searchController;
#property (nonatomic, strong) NSMutableArray *searchResults;
#property (nonatomic, strong) NSMutableArray *selectedCell;
#property (nonatomic, strong) NSMutableArray *selectedIngredients;
//I added this property to keep track of the selected row
#property (strong,nonatomic) NSIndexPath *selectedPath;
#end
#implementation FilterViewController {
NSArray *_locations;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedIngredients = [NSMutableArray array];
self.selectedCell = [NSMutableArray array];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.lastIndexPath = [defaults objectForKey:#"lastIndexPathUsed"];
// Create a new JSONLoader with a local file URL
JSONLoaderIngreds *jsonLoader = [[JSONLoaderIngreds alloc] init];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"locations" withExtension:#"json"];
// There's no transition in our storyboard to our search results tableview or navigation controller
// so we'll have to grab it using the instantiateViewControllerWithIdentifier: method
UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:#"FilterViewSearchResultsNavController"];
// Our instance of UISearchController will use searchResults
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
// The searchcontroller's searchResultsUpdater property will contain our tableView.
self.searchController.searchResultsUpdater = self;
// create the searchBar programatically.
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x,
self.searchController.searchBar.frame.origin.y,
self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;
//Sets LocationsViewController as presenter for LocationDetailViewController after searxh results dsiplayed
//and selected.. Required so searchbar doesn't show in detailsview after segue, and instead, default nav
//controller back button displays.
self.definesPresentationContext = true;
// Load the data on a background queue...
// As we are using a local file it's not really necessary, but if we were connecting to an online URL then we'd need it
//NSString *ingreds = [dict objectForKey:#"ingredients"]
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_locations = [jsonLoader ingredientsFromJSONFile:url];
// Now that we have the data, reload the table data on the main UI thread
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:YES];
});
}
// Just before showing the LocationViewController, set the selected Location object
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
LocationsViewController *vc = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
vc.location = [_locations objectAtIndex:indexPath.row];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewWillAppear:(BOOL)animated
{
//I added this if clause to select the row that was last selected
if (self.selectedPath != nil) {
[self.tableView selectRowAtIndexPath:self.selectedPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
#pragma mark - Table View Controller Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedPath = indexPath;
NSString *Ingredient = [_locations objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
[self.selectedCell removeObject:indexPath];
[self.selectedIngredients removeObject:Ingredient];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCell addObject:indexPath];
[self.selectedIngredients addObject:Ingredient];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(#"***************Selected Ingredients**************** %#", self.selectedIngredients);
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:[NSString stringWithFormat:#"%ld",(long)indexPath.section] forKey:#"lastIndexPathUsed"];
[userdefaults synchronize];
}
-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
return ([self.selectedCell containsObject:indexPath]) ? YES : NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *unifiedID = #"FilterCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
}
cell.textLabel.text = [_locations objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"ingredientsicon3232.png"];
//if the indexPath was found among the selected ones, set the checkmark on the cell
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_locations count];
}
UPDATE:
I managed to changed code as suggested to save selections to array in NSUserDefaults using the updated code below, but I still can't figure out the cellForRowAtIndexPath code needed to save/recall checkmarks.
How would I code cellForRowAtIndexPath to recall checkmarks?
Saving selections to array with this code:
ViewDidLoad code:
_selections = [NSMutableArray arrayWithArray:(NSArray *)[[NSUserDefaults standardUserDefaults] objectForKey:#"selections"]];
if(_selections == nil){
_selections = [[NSMutableArray alloc] init];
}
didSelectRowAtIndexPath code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
{
if ([_selections containsObject: cell.textLabel.text] == NO){
[_selections addObject:cell.textLabel.text];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
[_selections removeObject:cell.textLabel.text];
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSLog(#"***************Selected Ingredients**************** %#", _selections);
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:_selections forKey:#"selections"];
[userdefaults synchronize];
NSLog(#"-------------NSUserDefaults------------%#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation])
}
Your solution is pretty straight forward.
First thing is you need to save an array of selected index paths to your user default object instead of just last selected path.
Whenever a user selects or deselects a row. Add and remove objects from the same array and save it back to your user defaults.
In your cellForRowAtIndexPath, check whether your index path exists in the array saved in the user defaults. If it exists, select the row with checkmark otherwise leave it as it is.
Hope this helps.
I think instead of working with those indexPath, i recommended you to work straight with the data itself, add a Bool property to indicate the selection in your Ingredient class, then save the whole array in CoreData/Realm/NSUserDefault, that way is more correctly since your data can be change, lead to the selection indexPath that you save can be not correct anymore
I need to delete a row in my tableview to update my changes.
I have a delete button in each cell (tableViewCellController) - look at the picture.
Screenshot
After I click the delete button, the UI button method calls the delegated method in tableViewController. The delete method update the data source (my model) and i want to update also the screen (now the method reload all the data, but I want to update the new change - delete row from screen).
I tried to do this with the following function but i don't have a sender (as i said the button is pressed in the cell, but I actually make the change on the tableView)
Function:
- (IBAction)contactDelete:(id)sender{
[[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.superview];
[self.tableView deleteRowsAtIndexPaths:
[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
favoritesTableViewCell:
#import "favoritesTableViewCell.h"
#import "ModelUser.h"
#implementation favoritesTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)favoritesDeleteFromFav:(id)sender {
[[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
[self.delegate onFavDeleteClick];
}
favoritesTableViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.actualLoggedUser = [NSString stringWithFormat:#"2"];
[self reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)reloadData {
NSLog(#"Favorites tab was loaded");
//get id of my favorite contacts
myFavListId = [[ModelUser instance] getUser:self.actualLoggedUser].contactsFavoriteList;
//get data of my favorites contacts
myFavListContactsData = [[NSMutableArray alloc] init];
for (int i=0; i < [myFavListId count] ; i++) {
User* us = [[ModelUser instance] getUser:([myFavListId objectAtIndex:i])];
[myFavListContactsData addObject:us];
}
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (void) viewDidAppear:(BOOL)animated {
[self reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return myFavListContactsData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
favoritesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"favoriteCell" forIndexPath:indexPath];
User *us = [myFavListContactsData objectAtIndex:indexPath.row];
//setting cell data
cell.actualLoggedUser = self.actualLoggedUser;
cell.contactUserId = us.userId;
cell.contactName.text = [NSString stringWithFormat:#"%# %#",us.fname,us.lname];
[cell.contactImage setImage: [UIImage imageNamed:us.imageName]];
cell.delegate = self;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
User *us = [myFavListContactsData objectAtIndex:indexPath.row];
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
userDetailsProfile* udVC = [sb
instantiateViewControllerWithIdentifier:#"userDetailsProfile"];
udVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
udVC.userDetailId = [NSString stringWithFormat:#"%#", us.userId];
[self showViewController:udVC sender:self];
}
- (void)onFavDeleteClick {
[self reloadData];
}
#end
You need to update datasource and delete the cell in your delegate callback that is invoked from your cell class (onFavDeleteClick delegate method of favoritesTableViewCell class, in your case).
The process should be something like this:
In your "favoritesTableViewCell.h", declare a protocol containing onFavDeleteClick method. I think you have already done with this step. What you need to do is to update the method signature as -(void) onFavDeleteClick:(favoritesTableViewCell*)cell.
From "favoritesTableViewCell.m" call favoritesDeleteFromFav method like this:
-(IBAction)favoritesDeleteFromFav:(id)sender {
[self.delegate onFavDeleteClick:self];
}
Now in your view controller where the main UITableView exists implement the callback method like this:
-(void)onFavDeleteClick:(favoritesTableViewCell*)cell {
//update model
[[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
//update table view
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (indexPath) {
[self.tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
And this is everything you need to do to get your desired effect.
I'm developing a shopping app, in which i'm implementing a shopping cart. In my app, i need to increase the product quantity when the plus button is clicked and reduce the product quantity when the minus button is clicked. Here my problem is, when i click the plus button, all text field value is changing in the tableviewcell. Help me,below is
plus button action method
-(IBAction)plusBtn:(UIButton*)plus
{
[self.tbleView beginUpdates];
UITableViewCell *clickedCell = (UITableViewCell *)[[plus superview] superview];
NSIndexPath *clickedButtonIndexPath = [self.tbleView indexPathForCell:clickedCell];
plus.tag = clickedButtonIndexPath.row;
quantity.tag = clickedButtonIndexPath.row;
_curr =_curr+1;
quantity.text = [NSString stringWithFormat:#"%d",_curr];
[self.tbleView endUpdates];
[self.tbleView reloadData];
}
like this
getting..
can u check this functionality. it will useful for you. i hope it.
- (void)viewDidLoad
{
[super viewDidLoad];
quantity =[NSMutableArray new];
occu_list = [[NSArray alloc] initWithObjects:#"Occupation", #"two", #"three", #"four", #"five", #"six", #"seven", #"eight", nil];
for(int i=0;i<[occu_list count];i++)
{
[quantity addObject:#"0"];
}
click_textView=[[UIView alloc]init];
click_textView.frame=self.view.frame;
[self.view addSubview:click_textView];
[self tableviewone];
}
-(void)tableviewone
{
tbl_view = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
tbl_view.delegate = self;
tbl_view.dataSource = self;
tbl_view.backgroundColor = [UIColor whiteColor];
tbl_view.layer.borderColor=[[UIColor orangeColor]CGColor];
tbl_view.layer.borderWidth=2.0f;
tbl_view.layer.cornerRadius=5.0f;
tbl_view.layer.masksToBounds=YES;
[click_textView addSubview:tbl_view];
}
-(IBAction)check_btn_action:(id)sender
{
UIButton *btntag=(UIButton*)sender;
NSLog(#"%li",(long)btntag.tag);
NSLog(#"%#",[quantity objectAtIndex:btntag.tag]);
int ad=[[quantity objectAtIndex:btntag.tag]integerValue];
ad=ad+1;
[quantity removeObjectAtIndex:btntag.tag];
[quantity insertObject:[NSString stringWithFormat:#"%i",ad] atIndex:btntag.tag];
[tbl_view reloadData];
}
Write action method for your button and then inside that just use this one line of code for setting your value to the textfield:-
self.yourTxtFld.text=#"yourString"
Don`t write the Button action in ViewController,
write it in the CustomCell class.
example:
in the CustomCell.m
- (IBAction)plusButtonDidClicked:(UIButton *)sender {
int i = [self.textField.text intValue];
i++;
self.textField.text = [NSString stringWithFormat:#"%d", i];
}
Then back to the ViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
return cell;
}
It will be work well.
-(IBAction)plusBtn:(UIButton*)plus
{
UITableViewCell *clickedCell = (UITableViewCell *)[[plus superview] superview];
_curr=_curr+1;
UITextField *qty = (UITextField *) [clickedCell viewWithTag:90];
qty.text=[NSString stringWithFormat:#"%d",_curr];
}
Hi This is my first iPad app and trying to port my iphone app to iPad.
I have followed all the tutorials from http://www.raywenderlich.com/ still having a problem.
Also review this question and still having the problem . Splitviewcontroller with two tableviews, delegate problem
Basically, I have two UITableViewControllers in SplitViewController and when I click the tableview cell in root view controller, I want to populate the details in DetailsViewController in Right side on another Tableview.
The problem is I can manage to pass the array data from but I can't call tableview reload method.
Here is the code
LeftViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == 0){
NSLog(#"Row 0 Pressed");
RightViewController *rightvc = [self.storyboard instantiateViewControllerWithIdentifier:#"displayenglish"];
_locallayleft = [ConversationDatabase database].conversationsInfos;
NSLog(#"Just pushed the array");
rightvc.detailItem = _locallayleft;
rightvc.title = #"Greetings";
}
else if (row == 1) {
NSLog(#"Row 1 Pressed");
RightViewController *rightvc = [self.storyboard instantiateViewControllerWithIdentifier:#"displayenglish"];
_locallayleft = [ConversationDatabase database].conversationsInfosgeneral;
rightvc.detailItem = _locallayleft;
rightvc.title = #"General Conversation";
}
-----------------------------------------------------------------------------------------
RightViewController
- (void)setDetailItem:(NSArray *)newDetailItem
{
if(_detailItem != newDetailItem) {
_detailItem = newDetailItem;
[self configureView];
}
}
- (void)configureView
{
if (self.detailItem) {
self.locallay = self.detailItem;
_listOfCoversation = [[NSMutableArray alloc] init];
for (ConversationInEnglish *c in _locallay)
{
NSString *english = c.english;
NSLog(#"setDetails Item get called");
NSLog(#"%#",english);
[_listOfCoversation addObject:english];
}
[self.tableView reloadData];
NSLog(#"Trying to reload TableView");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_locallay count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"English";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
ConversationInEnglish *con = _locallay [indexPath.row];
_englishLabel = (UILabel *) [cell viewWithTag:200];
_englishLabel.text = con.english;
NSLog(#"My data from cell %#",con.english );
[_englishLabel setFont:[UIFont fontWithName:#"Open Sans" size:22]];
_myanmarLabel = (UILabel *) [cell viewWithTag:300];
[_myanmarLabel setFont:[UIFont fontWithName:#"TharLon" size:17]];
_tonebasedLabel = (UILabel *) [cell viewWithTag:400];
_tonebasedLabel.text = con.tone_based;
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"tableviewcell.png"]];
self.tableView.backgroundColor = background;
return cell;
}
It looks like when you tap a row in the table on the left, instead of updating the table on the right, you're instantiating a whole new table from the storyboard instead, but not replacing the one on the right with it.
There isn't enough context here to say exactly how to fix it, but what you'd want to do is when you tap a row in the table on the left, update the table on the right by setting its detailItem property.
You'll need access to the other table view. There are a few ways to do this depending on how you've got your application set up - if you're using the same left table view on both the iPhone and iPad then you'll probably need some conditional code to locate it, for example:
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
DetailViewController *detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
detailViewController.detailItem = newDetailItem;
}
Or you could configure it through the storyboard. Either way, the key is to find and update the existing table view instead of instantiating a new one from the storyboard.