'Can't get this to work at all ! I had a nice simple class for a TableView that took up the entire screen, but decided i'd slice it in half, so the table only took the top half of the screen. This is my code:
This is the TableSplitViewController.h
#interface TableSplitViewController : UIViewController
{
ChildrenTableViewController *firstController;
IBOutlet UITableView *firstTable;
}
This is the TableSplitViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
if (firstController == nil) {
firstController = [[ChildrenTableViewController alloc] init];
}
[firstTable setDataSource:firstController];
[firstTable setDelegate:firstController];
firstController.view = firstController.tableView;
}
This is the ChildrenTableViewController.h:
#interface ChildrenTableViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *childname;
#end
This is the ChildrenTableViewController.m:
#implementation ChildrenTableViewController
#synthesize childname;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.childname = [[NSMutableArray alloc]
initWithObjects:#"Oliver",
#"Stuart",
#"Ross",
#"Alex", nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowChildrenDetails"]) {
ChildrenDetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
detailViewController.childrenDetailModel = [[NSArray alloc]
initWithObjects: [self.childname objectAtIndex:[myIndexPath row]], nil];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (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 [self.childname count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"childrenTablecell";
ChildrenTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[ChildrenTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Configure the cell..
cell.childname.text = [self.childname objectAtIndex:[indexPath row]];
return cell;
}
This is ChildrenTableViewCell.h:
#interface ChildrenTableViewCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UILabel *childname;
#end
This is ChildrenTableViewCell.m:
#implementation ChildrenTableViewCell
#synthesize childname;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
This is a print screen of the storyboard layout. I've attached the label with the childname so it should print the name out :
This is a print screen of it running, its weird it has the 4 rows, i can select them, but the labels are not being printed :
![Screenshot 2][2]
Fully appreciate all the help and support you can provide. Thanks :)
[2]http://puu.sh/1USzX/512ede1328
Thanks to IOSDEV i managed to find out why it was doing it.
cell.childname.text = [self.childname objectAtIndex:[indexPath row]];
NSLog(#"%#",[self.childname objectAtIndex:[indexPath row]]);
NSLog(#"%#",cell.childname.text);
This is what i put into my code.
and this is what i got:
2013-01-30 15:33:52.465 TableViewStory[17509:907] Oliver
2013-01-30 15:33:52.467 TableViewStory[17509:907] (null)
2013-01-30 15:33:52.470 TableViewStory[17509:907] Stuart
2013-01-30 15:33:52.472 TableViewStory[17509:907] (null)
2013-01-30 15:33:52.473 TableViewStory[17509:907] Ross
2013-01-30 15:33:52.474 TableViewStory[17509:907] (null)
2013-01-30 15:33:52.476 TableViewStory[17509:907] Alex
2013-01-30 15:33:52.477 TableViewStory[17509:907] (null)
So as you can see cell.childname.text is equal to null, this is the reason why the text isn't coming up :P I don't understand why it is though.
You should use the initWithStyle in the TableSplitViewController.m
firstController = [[ChildrenTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
For some reason when I didn't use it to init the tableView I had some similar problems.
Related
So Im doing a little mini project, what Im trying to do is create something similar to a note taking app. What im doing is allowing the user to type in the title of their note in a text field, then adding that to a UITableViewCell, then when they click on that cell it takes them to another view where they can edit their title, save it and then return them to the original view but with a different title then the one they originally entered.
Here are my current issues
1) How do I save the new titles after the app is closed? (When I add a new title then restart the simulator the UITableView is empty)
2) How do I permanently change the UITableViewCell once the user edits the title? (The titles text goes into the text field but when I edit the title and hit save and go back to the original view controller the original title is still there)
Here is my code for the first view controller:
#import "TestViewController.h"
#interface TestViewController (){
}
#end
#implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
_array = [[NSMutableArray alloc] initWithObjects:#"First Note", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btn:(id)sender {
[_array insertObject:self.fld.text atIndex:0];
[_tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
if (cell == nil){
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = [_array objectAtIndex:indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"segue1"]){
Test1ViewController *vc = [segue destinationViewController];
Connecter *connectorClass = [[Connecter alloc] init];
connectorClass.stringToPass = self.array[self.tableView.indexPathForSelectedRow.row];
vc.connectorClass = connectorClass;
}
}
Heres my code for the second view controller:
#import "Test1ViewController.h"
#interface Test1ViewController ()
#end
#implementation Test1ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_label.text = _connectorClass.stringToPass1;
_textView.text = _connectorClass.stringToPass;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button1:(id)sender {
[self performSegueWithIdentifier:#"segue2" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segue2"]){
Test2ViewController *vc1 = [segue destinationViewController];
Connecter *connectorClass = [[Connecter alloc] init];
connectorClass.stringToPass1 = _textView.text;
vc1.connectorClass = connectorClass;
}
}
And here is a third view controller I used as a middle man to pass information through from the testviewcontroller to the test1viewcontroller
#import <UIKit/UIKit.h>
#interface Connecter : UIViewController
#property (nonatomic, strong) Connecter *connectorClass;
#property (nonatomic, strong) NSString *stringToPass;
#property (nonatomic, strong) NSString *stringToPass1;
#end
I'm having a bit confusion with the UITableViewCellStyle. I want to create a custom UITableViewCell like this:
But the text and the image not appear in the cell when I run the app. In Storyboard,I've put the TableView Style to 'Custom'.
What am I doing wrong?
MainTableViewController
#import "MainTableViewController.h"
#import "CustomMainCell.h"
static NSString *CellIdentifier = #"MainCell";
#interface MainTableViewController ()
//
#property(nonatomic, strong) NSArray *dataSource;
//
#property(nonatomic, strong) NSArray *iconsSource;
#end
#implementation MainTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Currículum Vitae";
// Inicializo los arrays con los datos
self.dataSource = #[#"PERFIL",#"EXPERIENCIA",#"EDUCACIÓN",#"HABILIDADES",#"INTERESES"];
self.iconsSource = #[#"perfil.png",#"experiencia.png",#"educacion.png",#"habilidades.png",#"intereses"];
// Register Class for Cell Reuse Identifier
[self.tableView registerClass:[CustomMainCell class] forCellReuseIdentifier:CellIdentifier];
// This will remove extra separators from tableview
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// Eliminio las líneas que separan las celdas
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (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 self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomMainCell *cell = (CustomMainCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.title.text = self.dataSource[indexPath.row];
cell.icon.image = self.iconsSource[indexPath.row];
return cell;
}
CustomMainCell.h
#interface CustomMainCell : UITableViewCell
//
#property (weak, nonatomic) IBOutlet UILabel *title;
//
#property (weak, nonatomic) IBOutlet UIImageView *icon;
#end
CustomMainCell.m
#import "CustomMainCell.h"
#implementation CustomMainCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.textLabel.textColor = [UIColor brownColor];
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
You shouldn't have:
// Register Class for Cell Reuse Identifier
[self.tableView registerClass:[CustomMainCell class] forCellReuseIdentifier:CellIdentifier];
because the cell is registered from the storyboard when the view controller is unarchived. By registering the class you are removing the archive (NIB) registration.
Additionally:
if (cell == nil) {
cell = [[CustomMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
shouldn't be required as you will always get a valid cell back (because the identifier is registered)
I'm unproficient at Objective-c and am trying to build a basic notePad app .
i've finished the first part and am getting this Exception :
2014-04-21 15:20:56.983 ToDoList[1261:60b] -[NazorienAddToDoItemViewController
addToDoItemNameField:]: unrecognized selector sent to instance 0x8c4a990
2014-04-21 15:20:57.054 ToDoList[1261:60b] *** Terminating app due to uncaught
xception 'NSInvalidArgumentException'', reason: '-[Nazorie
nAddToDoItemViewController addToDoItemNameField:]: unrecognized selector sent to
instance 0x8c4a990'"
this is the class i use for table view of the items.
NazorienToDoItemTableViewController.h class :
#import "NazorienToDoListTableViewController.h"
#import "NazorienToDoItem.h"
#import "NazorienAddToDoItemViewController.h"
#interface NazorienToDoListTableViewController ()
#end
#implementation NazorienToDoListTableViewController
-(void) loadInitialData {
NazorienToDoItem *item1 = [[NazorienToDoItem alloc] init];
item1.itemName = #"finish this app";
//Adding the to do item to the array "toDoItems"
[self.toDoItems addObject:item1];
NazorienToDoItem *item2 = [[NazorienToDoItem alloc] init];
item2.itemName = #"go to the super market";
[self.toDoItems addObject:item2];
}
-(void)unwindToList:(UIStoryboardSegue *)segue
{
NazorienAddToDoItemViewController *source = [segue sourceViewController];
NazorienToDoItem *item = source.addToDoItem;
if(item!=nil)
{
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc]init];
[self loadInitialData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
return [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
// Configure the cell...
NazorienToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if(toDoItem.completed){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - Table view delegate
//mark completed for selected rows
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath {
//deselect the row so that cell is only tapped and not selected for editing
[tableView deselectRowAtIndexPath:indexPath animated:NO];
//returns the item at the index row of the table
NazorienToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
//marks if item is completed or uncompleted
tappedItem.completed = !tappedItem.completed;
//reload the row
[tableView reloadRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
#end
this is the class i use for adding items, it holds only 1 text field and cancel and done buttons.
NazorienAddToDoItemViewController.m:
#import "NazorienAddToDoItemViewController.h"
#interface NazorienAddToDoItemViewController()
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
#end
#implementation NazorienAddToDoItemViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if (sender != self.doneButton) return;
if (self.textField.text.length>0){
self.addToDoItem = [[NazorienToDoItem alloc] init];
self.addToDoItem.itemName = self.textField.text;
self.addToDoItem.completed = NO;
//closes the keyboard
[self.textField resignFirstResponder];
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Edit
the error happens when i press the "done" or "cancel" buttons in my navigation bar of the NazorienAddToDoListViewAdapter class , and only after i open the keyboard to write a new item name and press either buttons....
#import "sideTableViewController.h"
#interface sideTableViewController ()
{
NSArray *colours;
}
#end
#implementation sideTableViewController
#synthesize colorNames;
#synthesize sideTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.sideTableView.delegate= self;
self.sideTableView.dataSource=self;
colorNames = [NSArray arrayWithObjects:#"Archie",#"Sethi",#"Rajan" ,#"Deepak" ,nil];
}
- (NSInteger)sideTableView:(UITableView *)sideTableView numberOfRowsInSection:(NSInteger)section
{
return [colorNames count];
}
- (UITableViewCell *)sideTableView:(UITableView *)sideTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [sideTableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text =[colorNames objectAtIndex:indexPath.row];
// NSLog(#"the indexpath is %#",indexPath);
return cell;
}
- (void)sideTableView:(UITableView *)sideTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [sideTableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[sideTableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
and this is the header file...
#import <UIKit/UIKit.h>
#interface sideTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *colorNames;
}
#property (strong, nonatomic) IBOutlet UITableView *sideTableView;
-(IBAction)showMessage;
#property (nonatomic, retain) NSArray *colorNames;
#end
I'm trying to get an image as a background to an existing tableview.
i'm getting an exception regarding: [sideTableViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7593640
please help i'm new in IOS programming!
may this code help you simply add the below code to just above
- (NSInteger)sideTableView:(UITableView *)sideTableView numberOfRowsInSection:(NSInteger)section
add this code
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
I did an empty project on UITable Cells and it worked. Using the same code I added in to an existing project where there are multiple xib linked. When navigating from another xib to this page, the table cells did appear but the content and the required number of cells did not run.
Below is the code of my current project:
OptionViewController.m:
#import "OptionViewController.h"
#import "FishAppDelegate.h"
#import "MainViewController.h"
#import "PetFishViewController.h"
#import "MarineFishViewController.h"
#interface OptionViewController ()
#end
#implementation OptionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title =#"Choose a category :)";
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)PetFish:(id)sender
{
PetFishViewController *petFish = [[PetFishViewController alloc]initWithNibName:nil bundle:nil];
[[self navigationController] pushViewController:petFish animated:YES];
}
// this is the sub class containing table cells
-(IBAction)MarineFish:(id)sender
{
MarineFishViewController *MarineFish = [[MarineFishViewController alloc]initWithNibName:nil bundle:nil];
[[self navigationController] pushViewController:MarineFish animated:YES];
}
#end
At the current sub class: marine fish
MarineFishController.h
#import <Foundation/Foundation.h>
#interface MarineFishViewController : UITableViewController
{
NSMutableArray *fishList;
}
#end
MarineFishController.m :
#import "MarineFishViewController.h"
#import "MarineFish.h"
#implementation MarineFishViewController
{
/*NSArray *tableData;
NSArray *thumbnail;
*/
}
-(id) init{
//call the superclass
self = [super initWithStyle:UITableViewStyleGrouped];
if(self){
fishList = [[NSMutableArray alloc] init];
MarineFish *Item1 = [[MarineFish alloc] initWithName:#"Shark" imageName:#"Sea.jpg"];
[fishList addObject:Item1];
}
return self;
}
-(id)initWithStyle:(UITableViewStyle)style{
return [self init];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [fishList count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create an instance of uitableviewcell
//check for a reusable cell first, use if exist
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
//if there is no reusable cell of this type,create new one
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"UITableViewCell"];
}
//set text on the cell
MarineFish *p = [fishList objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[[NSString alloc] initWithFormat:#"%#",[p fishName]]];
}
#end
Regarding to the table. I only added one item to the fish list which is item1 and hence should only have one cell in display.
You don't need to override init and initWithStyle: methods. Move your code from init to viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
fishList = [[NSMutableArray alloc] init];
MarineFish *Item1 = [[MarineFish alloc] initWithName:#"Shark" imageName:#"Sea.jpg"];
[fishList addObject:Item1];
}
EDIT
Error was so oblivious...
-(UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
Must return a UITableViewCell object. So put return cell; at the end. Here is edited MarineFishViewController.m.