I'm just using code but not xib.
here's the code
tableviewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
[self createData];
- (void)createData
{
Model *user = [[Model alloc]init];
[user setName:#"a"];
[user setWeibo:#"a"];
Model *user2 = [[Model alloc]init];
[user2 setName:#"b"];
[user2 setWeibo:#"b"];
tableData = [NSMutableArray arrayWithObjects:user,user2, nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Model *userData = [tableData objectAtIndex:indexPath.row];
[cell.name setText:userData.name];
return cell;
}
cell.name is label.but it cannot show anything
please tell me how to let the custom tableviewcell's label to show something
I'm using UITableViewController so I guess i don't have to set tableviewdelegate and tableviewdatasorce.
and here's the tableviewcell's detail:
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
self.name = [[UILabel alloc]initWithFrame:CGRectMake(70, 15, 75, 30)];
[self addSubview:self.name];
}
How do you pass data from a tableview Controller with sections to a ViewController? I can do it when I'm not using sections, but the program crashes when I use sections? And I don't understand.
This is the error message I get and it cranes with a SIGABIT message on this line:
NSString *mytempName = [NSString stringWithString:[tempObject charName]];
Error message:
2014-07-24 06:38:05.465 Passing_Data_With_Two_Sections[469:60b] -[__NSArrayM charName]: unrecognized selector sent to instance 0x8f0d230
2014-07-24 06:38:05.501 Passing_Data_With_Two_Sections[469:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM charName]: unrecognized selector sent to instance 0x8f0d230'
Here is the code from the TableView Controller
#import "myTableView.h"
#interface myTableView ()
#end
#implementation myTableView
NSMutableArray *myHeadersArray;
NSMutableArray *myFightersArray;
NSMutableArray *myLadiesArray;
NSMutableArray *myArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
myHeadersArray = [[NSMutableArray alloc]initWithObjects:#"Fighters", #"Ladies", nil];
myFightersArray = [[NSMutableArray alloc]init];
myLadiesArray = [[NSMutableArray alloc]init];
objectFile *myObject = [[objectFile alloc]init];
myObject.charName = #"Peter Pan";
[myFightersArray addObject:myObject];
myObject = [[objectFile alloc]init];
myObject.charName = #"Mikey Mouse";
[myFightersArray addObject:myObject];
myObject = [[objectFile alloc]init];
myObject.charName = #"Mrs Duck";
[myLadiesArray addObject:myObject];
myObject = [[objectFile alloc]init];
myObject.charName = #"Mini Mouse";
[myLadiesArray addObject:myObject];
myArray = [NSMutableArray arrayWithObjects:myFightersArray, myLadiesArray, nil];
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [myHeadersArray objectAtIndex:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [myHeadersArray count];
//This seems to crash if you go above 2. Which I assume is somehow tied in with the sections.
//Adding additional names now no longer crash. But if I changed it to myArray then it crashes
//bitching it being greater 2.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCell" forIndexPath:indexPath];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = ((myTempObjectFile*)[[myArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]).charName;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ViewController *vc = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
int myrow = [path row];
myTempObjectFile *tv = [myArray objectAtIndex:myrow];
vc.tempObject = tv;
}
#end
And here is the code from my ViewController:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myLabelOutput;
#synthesize tempObject;
- (void)viewDidLoad
{
NSString *mytempName = [NSString stringWithString:[tempObject charName]];
[myLabelOutput setText:mytempName];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This happens because you try to invoke method charName for an array.
As I see you store arrays in array and you should change implementation to:
myTempObjectFile *tv = [[myArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
vc.tempObject = tv;
or just
myTempObjectFile *tv = myArray[indexPath.section][indexPath.row]
vc.tempObject = tv;
This answer to your question.
But I think that here we need to refactor: not very beautiful to store arrays in an array, it is better to make an abstract object for this data structure. And tempObject name does not bear any semantic meaning.
Basically what i want to do is i want to fill a table view with strings from an array. I've made a second UIviewcontroller and a custom cell with that show an image and two other labels. But when i run the code, there is no error messages but the tableview is blank, almost like its not recognizing the code at all. I have declared a delegate and datasource for it in the .h file.
#import "ViewController4.h"
#import "Cell.h"
#import "XMLparse.h"
#interface ViewController4 ()
#end
#implementation ViewController4
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(NSMutableArray *)objects {
if (!Datarray) {
Datarray = [[NSMutableArray alloc]init];
}
return Datarray;
}
- (void)viewDidLoad
{
[super viewDidLoad];
Datarray = [[NSMutableArray alloc]initWithObjects:#"USD",#"EUR",#"JPY",#"BGN",#"CZK",#"DKK",#"GBP",#"HUF",#"LTL",#"PLN",#"RON",#"SEK",#"CHF",#"NOK",#"HRK",#"RUB",#"TRY",#"AUD",#"BRL",#"CAD",#"CNY",#"HKD",#"IDR",#"ILS",#"INR",#"KRW",#"MXN",#"MYR",#"NZD",#"PHP",#"SGD",#"THB",#"ZAR", nil];
Tableview.delegate = self;
XMLparse *datee = [[XMLparse alloc]init];
[datee loadDataFromXML];
Date.text = [NSString stringWithFormat:#"Last updated: %#",[datee tid]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableview:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section {
return [Datarray count];
}
- (CGFloat)tableView:(UITableView *)tableview heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Cell";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// cell.ratename.text = [tableData objectAtIndex:indexPath.row];
// cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.currencyname.text = [Datarray objectAtIndex:indexPath.row];
return cell;
}
#end
You have to set self to datasource property of UITableView. I suggest you the following code edited.
- (void)viewDidLoad
{
[super viewDidLoad];
Datarray = [[NSMutableArray alloc]initWithObjects:#"USD",#"EUR",#"JPY",#"BGN",#"CZK",#"DKK",#"GBP",#"HUF",#"LTL",#"PLN",#"RON",#"SEK",#"CHF",#"NOK",#"HRK",#"RUB",#"TRY",#"AUD",#"BRL",#"CAD",#"CNY",#"HKD",#"IDR",#"ILS",#"INR",#"KRW",#"MXN",#"MYR",#"NZD",#"PHP",#"SGD",#"THB",#"ZAR", nil];
Tableview.delegate = self;
Tableview.dataSource = self; // <---- added code for setting datasource.
XMLparse *datee = [[XMLparse alloc]init];
[datee loadDataFromXML];
Date.text = [NSString stringWithFormat:#"Last updated: %#",[datee tid]];
}
My cellForRowAtIndexPath does not fire everytime
I set dataSource and delegate to the file owner.
I also set referencing outlet.
I also try tbl.delegate=self; and tbl.datasource=self; but it does not work.
What can I do?
-(void)load
{
[name resignFirstResponder];
[salary resignFirstResponder];
name.text=#"";
salary.text=#"";
[tblViewController.data removeAllObjects];
NSString *insertStatement = [NSString stringWithFormat:#"select * from emp"];
sqlite3_stmt *statement;
NSString *path= [self getDBPath];
if (sqlite3_open([path UTF8String], &db) == SQLITE_OK)
{
if (sqlite3_prepare_v2(db, [insertStatement UTF8String], -1,&statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSMutableDictionary *record=[[NSMutableDictionary alloc]init];
NSString *nm= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)];
NSString * sl= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,1)];
[record setValue:nm forKey:#"name"];
[record setValue:sl forKey:#"salary"];
NSLog(#"%# %#",nm,sl);
[tblViewController.data addObject:record];
NSLog(#"%#",tblViewController.data);
}
sqlite3_finalize(statement);
}
sqlite3_close(db);
}
NSLog(#"%lu",(unsigned long)[tblViewController.data count]);
[tblViewController.tbl reloadData];
}
//This code is of tableviewcontroller.m
#import "tableViewController.h"
#import "ViewController.h"
#interface tableViewController ()
#end
#implementation tableViewController
#synthesize tbl,data;
#synthesize viewOfTable;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(#"only for check");
[self setup];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//[self setup];
tbl.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-( NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-( NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[[data objectAtIndex:indexPath.row] valueForKey:#
"name"];
cell.detailTextLabel.text=[[data objectAtIndex:indexPath.row]
valueForKey:#"salary"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//name.text =[[data objectAtIndex:indexPath.row] valueForKey:#"name"];
// salary.text=[[data objectAtIndex:indexPath.row] valueForKey:#"salary"];
}
- (void)setup
{
//Store the whole view in uiView...mac
// [[NSBundle mainBundle] loadNibNamed:#"RegistrationViewController" owner:self options:nil];
viewOfTable=[[UIView alloc]initWithFrame:self.view.frame];
[viewOfTable addSubview:self.view];
self.viewOfTable.translatesAutoresizingMaskIntoConstraints=YES;
}
Please solve my problem.
You didn't get anything then please tell me.
You need to set Table DataSource in order to tableView to construct table rows.
#import "tableViewController.h"
#import "ViewController.h"
#interface tableViewController () <UITableViewDataSource,UITableViewDelegate>
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tbl.dataSource = self
tbl.delegate = self;
}
Or you can check if you have a fault in your data array, because if its size is 0, there are no rows in a section and no cells within these rows as well and cellForRowAtIndexPath is never called...
You should set dataSource and delegate. When you do this, check tbl. Maybe it nil?
If not check what value returns
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
If you implement
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
check it.
No more options.
On pressing a disclosure button i want to push another view containing the details of the cell from which the disclosure button was pushed. I have the code below but dont know why it doesnt work. On clicking the disclosure button the programme does go into the tableView:accessoryButtonTappedForRowWithIndexPath function but doesnt bring up the view.
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
self.detailController.title = #"Single Contact";
NSMutableArray *array = [[NSMutableArray alloc] init];
if (indexPath.section == 0) {
array = self.friends;
}
else if (indexPath.section == 1) {
array = self.members;
}else{
array = self.invite;
}
NSDictionary *data = [array objectAtIndex:indexPath.row];
NSString *name = [data valueForKey:#"firstName"];
self.detailController.message = name;
[self alertMeWithString:#"THIS STRING"];
[self.navigationController pushViewController:self.detailController animated:YES];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
self.detailController = [[DSZContactDetailViewController alloc] init];
}
return self;
}