My Situation: i have a "homeviewcontroller", and here I'm wanting to acces a cell title of a tableview of another view. So the title of the cell apears in the homeviewcontroller when its appear.
So, i have a tableviewcontroller. Then i have my homeviewcontroller with a label what want to change the text to the cell title text.
PROBLEM: This is my code but it isnt working, the label apears empty.
Home View Controller Code:
Homeviewcontroller.h
...
#property (weak, nonatomic) IBOutlet UILabel *celltitle;
...
Homeviewcontroller.m
...
#import "tableviewcontroller"
#synthesize celltitle;
...
- (void)viewDidAppear:(BOOL)animated
{
Tableviewcontroller *hourscell = [tableviewcontroller alloc];
diahoy.text = hourscell.daylabel.text;
}
and my tableViewController (where i set the cel) method:
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Hours Cell";
CellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Hours *hour = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.daylabel.text = hour.dayselect;
return cell;
}
I want to make clear that my table is working correct, user can add cells without any problem.
Also, it would be great if you know how to get the titlelabel of a certain cell row.
Thanks a lot.
Think you forgot to initialise your tableViewController. You did only allocate it in your HomeViewController.m. And the label should be cellTitle instead of diahoy I guess:
- (void)viewDidAppear:(BOOL)animated
{
Tableviewcontroller *hourscell = [[Tableviewcontroller alloc] init];
celltitle.text = hourscell.daylabel.text;
}
Related
I have a UITableViewController which consists of a TableView and a UILabel. UITableViewSource feeds data to the TableView through custom UITableViewCell. UITableViewCell consists of a Stepper which can add items and the Label holds that value.
I want to update the UILabel in UIViewController when the user taps on the Stepper and i cannot seem to get that to work...
Any help/tips are greatly appreciated! Here is sample code from my test project, there is a bunch of unused code but this is just a test so i let it be there.
Thanks,
The Code is
My Custom Cell Class is "StepperDetailsCell".
#interface StepperDetailsCell : UITableViewCell
#property (nonatomic,weak) id <StepperDetailsCellDelegate> delegate;
#property (strong, nonatomic) IBOutlet UIImageView *imagegroceries;
#property (strong, nonatomic) IBOutlet UIStepper *objstepper;
#property (strong,nonatomic) IBOutlet UILabel *lblsteppervalue;
-(IBAction)stepperclicked:(UIStepper*)sender;
#end
and My table view class is named as "SecondViewController"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"Cell";
cell = (StepperDetailsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil){
cell = (StepperDetailsCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell.imagegroceries setImage:[UIImage imageNamed:[[self.didSelectedImages sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)]objectAtIndex:indexPath.row]]];
[cell.objstepper addTarget:self action:#selector(stepperValuechanges:) forControlEvents:UIControlEventValueChanged];
cell.lblsteppervalue.text = [self.quantityArr objectAtIndex:indexPath.row];
return cell;
}
pragma mark - Stepper Action Method
-(void)stepperValuechanges:(UIStepper *)sender{
CGPoint stepperPosition = [sender convertPoint:CGPointZero toView:self.tableView];
indexPath1 = [self.tableView indexPathForRowAtPoint:stepperPosition];
if (indexPath1 != nil )
{
float val = sender.value;
valueInt = (int)val;
[self.quantityArr replaceObjectAtIndex:indexPath1.row withObject:[NSString stringWithFormat:#"%i",valueInt]];
NSLog(#"%#",self.quantityMDict);
}
[self.tableView reloadData];
}
Here, Everything is working properly,but whenever new cell is loading from bottom of tableview, then that time new cell stepper act as disappeared cell stepper.
Finally After 3 days I solved my problem.
Just set the value for UIStepper before firing the action of the UIStepper Object to the corresponding Controller Object like the following way..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"Cell";
cell = (StepperDetailsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil){
cell = (StepperDetailsCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell.imagegroceries setImage:[UIImage imageNamed:[[self.didSelectedImages sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)]objectAtIndex:indexPath.row]]];
// I just set the previous value(this value, I am taken from the array named "quantityArr") to corresponding stepper, before firing the action to the controller object from the stepper object. That's it..
[cell.objstepper setValue:[[self.quantityArr objectAtIndex:indexPath.row] doubleValue]];
[cell.objstepper addTarget:self action:#selector(stepperValuechanges:) forControlEvents:UIControlEventValueChanged];
cell.lblsteppervalue.text = [self.quantityArr objectAtIndex:indexPath.row];
return cell;
}
I have a UITableView which contains the names of all countries.
The user can delete or edit the name of country anytime by taping on the cell.
My UITableView cell initially looks like this:
Now when user taps on it I am changing it like this:
I think I am following a very lame approach.Here is what I did:
Declared globally buttons to add:
UIButton *btnDeleteWithImage,*btnDeleteWithText,*btnEditWithImage,*btnEditWihtText; //buttons
And a NSMutableArray to keep track of indexPath.row
Now in my didSelectMethod I am doing this:
//To change the background
UIView *selectionBackground = [[UIView alloc] init];
selectionBackground.backgroundColor = [UIColor customColor];
cell.selectedBackgroundView = selectionBackground;
// to check which cell is pressed
if([indexPathCollection containsObject:index])
{
[btnDeleteWithImage removeFromSuperview];
[btnDeleteWithText removeFromSuperview];
[btnEditWihtText removeFromSuperview];
[btnEditWithImage removeFromSuperview];
[indexPathCollection removeObject:index];
[cell addSubview:btnDeleteWithImage];
[cell addSubview:btnDeleteWithText];
[cell addSubview:btnEditWithImage];
[cell addSubview:btnEditWihtText];
[indexPathCollection addObject:index];
}
else
{
[cell addSubview:btnDeleteWithImage];
[cell addSubview:btnDeleteWithText];
[cell addSubview:btnEditWithImage];
[cell addSubview:btnEditWihtText];
[indexPathCollection addObject:index];
}
But this is not working good.When I scroll table edit and delete button randomly occurs.
Did someone has better Idea how can achieve this in a very efficient way.
You can achieve this by creating a custom cell with your properties
CustomCell.h
#interface CustomCell : UITableViewCell
#property (nonatomic, strong) UIButton *btnDeleteWithImage;
#property (nonatomic, strong) UIButton *btnDeleteWithText;
#property (nonatomic, strong) UIButton *btnEditWithImage;
#property (nonatomic, strong) UIButton *btnEditWithText;
#end
initialize them in cell's init method keeping them hidden at first or you can do
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *customCellIdentifier = #"customCellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:customCellIdentifier];
// or you can initialize and add them to the cell here
}
//here you can modify them accordingly
return cell;
}
then the delegate method can be
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.btnDeleteWithImage setHidden:NO];
[cell.btnEditWithImage setHidden:NO];
}
the simplest way is that do not reuse the cell . & register your custom cell with same indentifire .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customCell* cell = [[customCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:#"indentifire"] ;
// manage your plooting here ..
return cell;
}
hope it works for you.
I have a tableview for entering the ingredients of a recipe in a recipe app. I want to be able to type in the ingredients in the tableview cell and then save it to display in another view controller - UILabel (dynamically created).
I found code to make an editable tableview where data can be inserted and deleted, but no code to be able to type in a tableview cell.
Is there a way to be able to type in a tableview cell? What is the best way to do it?
Additionally, all data entered needs to stored in the database using core data.
Here is a screenshot of my storyboard:
The tableview in the AddRecipeViewController is where I want to enter data. When I click save, the data should be displayed in RecipeDetailViewController Ingredients UIView.
Update:
I have created IngrdientsTableViewCell class for custom cell with a UITextField for entering text. But when I compile, the tableview does not display any textfield to type in. I have no idea what I am doing wrong. Please help!
Updated code:
IngrdientsTableViewCell.h
#interface IngredientsTableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UITextField *ingredientTextField;
#end
IngrdientsTableViewCell.m
- (void)awakeFromNib
{
// Initialization code
self.ingredientTextField = [[UITextField alloc]initWithFrame:CGRectMake(5, 5, 320, 39)];
self.ingredientTextField.autoresizingMask=UIViewAutoresizingFlexibleHeight;
self.ingredientTextField.autoresizesSubviews=YES;
self.ingredientTextField.layer.cornerRadius=10.0;
[self.ingredientTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.ingredientTextField setPlaceholder:#"Type Data Here"];
}
AddRecipeViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.ingredientItems.count;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Ingredient Cell";
IngredientsTableViewCell *ingredientCell = [self.ingredientsTableView dequeueReusableCellWithIdentifier:cellIdentifier];
// NSManagedObjectContext *managedObject = [self.ingredientItems objectAtIndex:indexPath.row];
if (ingredientCell == nil)
{
ingredientCell = [[IngredientsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ingredientCell.accessoryType = UITableViewCellAccessoryNone;
}
[ingredientCell addSubview:ingredientCell.ingredientTextField];
return ingredientCell;
}
Thanks for any help.
You should have a custom UITableViewCell with a UITextView in it.
You can add a textField/View to your tableViewCell and just handle the text events like you normally would. I do something similar with TextFields.
UITextField *textField;
cell.textLabel.text = NSLocalizedString(#"Name", #"Name");
textField = self.name = [self makeTextField:self.selectedUser.name];
textField.delegate = self;
self.name.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.name.keyboardType = UIKeyboardTypeDefault;
self.name.returnKeyType = UIReturnKeyNext;
self.name.tag = indexPath.row;
[cell addSubview:self.name];
You will have to format the text field with constraints depending on your setup, but ultimately you will want to do this.
So I've used this tutorial to populate a UITableView with custom cells that represent balances. When stepping through the code, I witness the correct amount of cells get created (only 4 with the current test data) and their labels' text set correspondingly.
My problem is when the table is displayed on the screen, only the first row/cell is displayed.
Any insight as to why this could be occurring would be greatly appreciated!
Removed old code.
BalanceCell.h:
#import <UIKit/UIKit.h>
#interface BalanceCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
#property (weak, nonatomic) IBOutlet UILabel *amountLabel;
#property (weak, nonatomic) IBOutlet UILabel *modifiedLabel;
#end
EDIT:
My TableView delegate methods are now as follows:
- (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 [_balances count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
BalanceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[BalanceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [_hex colorWithHexString:_themeColourString];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(BalanceCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
Balance *item = [_balances objectAtIndex:indexPath.row];
cell.nameLabel.textColor = _themeColour;
cell.nameLabel.text = item.name;
cell.amountLabel.textColor = _themeColour;
cell.amountLabel.text = [NSString stringWithFormat:#"%#%#", item.symbol, item.value];
cell.modifiedLabel.textColor = _themeColour;
cell.modifiedLabel.text = [NSString stringWithFormat:#"%#", item.modified];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 94;
}
As #Sebyddd suggested, I now register the NIB in the viewDidLoad method.
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"BalanceCell" bundle:nil] forCellReuseIdentifier:#"Cell"];
}
These changes may make my code more correct but still only the first cell is displayed.
If cells are getting created and returned properly I guess height is not being set propery. By default I beleive all cells have a height of 44. If your cell exceeds this height it might not get displayed.
You can tell the tableview to adjust height for every cell using (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath delegate
In that delegate just return your cells height.
EDIT:
You are using dequeueReusableCellWithIdentifier: which will return A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.
Instead use dequeueReusableCellWithIdentifier:forIndexPath: which will return A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.
You need to register the nib/class for that custom cell in viewDidLoad
Try this:
if (cell == nil) {
[tableView registerNib:[UINib nibWithNibName:#"BalanceCell" bundle:nil] forCellReuseIdentifier:#"Cell"];
cell = [[BalanceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
Use this tuto : http://www.appcoda.com/uitableview-tutorial-storyboard-xcode5/ , your tuto is a bit outdated, and hard to follow !
I'm using a table view to display an array of data and I would like to pass the value to update Labels and image view in the detail view controller. I actually created a custom cell and I loaded it in the Table view and on selecting a row i pushed a new custom view controller with labels and ImageView. I would like to use the values of selected cell to be displayed in the labels in custom view controller. The code compiles but the only thing is that the Labels and image view in the detail view doesn't get updated. I just don't know where I'm going wrong…I'm pretty much stuck with for quite sometime. below is the code in my app…
code in table view…
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.LocationLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.TimeLabel.text = [time objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];
//transfering control to detail view
[[self navigationController] pushViewController:trailsController animated:YES];
}
TrafficTableViewViewController.m
#synthesize trafficImage = _trafficImage;
#synthesize LocationLable = _LocationLable;
in the above code TrafficDetailViewController is a custom view controller I've created and it contains labels and UIImage. Please let me know where I'm going wrong...
Try this code, may it will solve your issue. the problem is that you are setting the properties after pushing the controller which is wrong. try the below code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
trailsController.trafficImage = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];
//transfering control to detail view
[[self navigationController] pushViewController:trailsController animated:YES];
}
EDIT
.h File
in .m File
Add the synthesizer if you are not adding yet.
#synthesize trafficImage = _trafficImage;
#synthesize LocationLable = _LocationLable;
Also Edit one more thing when you are trying to push Your TrafficDetailViewController
then please assign image to trafficImage.image not to trafficImage and string to LocationLabel.text because trafficImage is an ImageView not an Image and LocationLable is a label not string.So replace your line with following code
trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];
POST EDIT
in .h file
#interface TrafficDetailViewViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *trafficImage;
#property (strong, nonatomic) IBOutlet UILabel *LocationLable;
//Add these two more properties in TrafficDetailViewController
#property (strong, nonatomic) UIImage *image;
#property (strong, nonatomic) NSString *locationString;
#end
in .m File:
#synthesize trafficImage = _trafficImage;
#synthesize LocationLable = _LocationLable;
#synthesize image = _image;
#synthesize locationString = _locationString;
in viewDidLoad method:
- (void) viewDidLoad
{
self.trafficImage.image = _image;
self.LocationLabel.text = _locationString;
}
and then in Previous viewController where you have written the push code for TrafficDetailviewController replace the following code with yours
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
trailsController.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.locationString = [tableData objectAtIndex:indexPath.row];
//transfering control to detail view
[[self navigationController] pushViewController:trailsController animated:YES];
}
The reason behind this is like your ViewController is not loaded before pushing and we are trying to set imageView.image and Lable.text which I think is causing the issue. Please try this and let me know is it working now?
Enjoy coding!!
Change the order of your code that creates and pushes the trailsController. Create it, set the properties, and THEN push it.
That may or may not be the cause of your problem. Next you need to debug the trailsController. What do you do with the properties trafficImage and locationTable? Do you have custom setters, or do you use them in your viewDidLoad or viewWillAppear methods? Post the relevant code from those methods in the other view controller.
Your problem may be in the way you instantiate TrafficDetailViewViewController, using alloc init. This will create a controller with an empty view, and is probably not the one you want to push on screen. If you created this controller's view in a xib, then you should be using initWithNibName:bundle: instead of init. If you created it in a storyboard, then you should use [self.storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier"]. This line could be a problem too, I can't tell what you're trying to do here:
trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];
Is LocationLable a label, or is it a string? To populate a label, you should pass the string to the next controller, and have that controller set the text of its own label.