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]];
}
Related
i have a table view with section and i would like to start the table view with all section grouped like this:
Section1
Section2
Section3
When i tap on a section title, it expand the rows
Section1
Row1
Row2
Row3
Section2
Section3
i requested to all please see the answer if you dont like,just comment
this is for beginners....
this code also refered from net only and i changed few things
this link
creat class file...
ViewController.h file should be
#import <UIKit/UIKit.h>
#import "oneViewController.h"
#import "secondViewController.h"
#import "thirdViewController.h"
#interface ViewController : UIViewController
{
oneViewController *one;
secondViewController *second;
thirdViewController *third;
}
#end
ViewController.m file should be
#import "ViewController.h"
#import "Cell1.h"
#import "Cell2.h"
#interface ViewController()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *dataList;
}
#property (assign)BOOL isOpen;
#property (nonatomic,retain)NSIndexPath *selectIndex;
#property (nonatomic,retain)IBOutlet UITableView *expansionTableView;
#end
#implementation ViewController
#synthesize isOpen,selectIndex;
- (void)dealloc
{
[dataList release];
dataList= nil;
self.expansionTableView = nil;
self.isOpen = NO;
self.selectIndex = nil;
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nil];
if (self) {
one=[[oneViewController alloc]init ];
second=[[secondViewController alloc]init];
third=[[thirdViewController alloc]init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"array" ofType:#"plist"];
dataList = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(#"%lu",(unsigned long)dataList.count);
self.expansionTableView.sectionFooterHeight = 0;
self.expansionTableView.sectionHeaderHeight = 0;
self.isOpen = NO;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [dataList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.isOpen) {
if (self.selectIndex.section == section) {
return [[[dataList objectAtIndex:section] objectForKey:#"list"] count]+1;
}
}
return 1;
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",[[dataList objectAtIndex:indexPath.section]objectForKey:#"list"] );
if (self.isOpen&&indexPath.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"Cell2";
Cell2 *cell = (Cell2*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
if ([[dataList objectAtIndex:indexPath.section]objectForKey:#"list"])
{
NSArray *list = [[dataList objectAtIndex:indexPath.section] objectForKey:#"list"];
cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
return cell;
}else
{
static NSString *CellIdentifier = #"Cell1";
Cell1 *cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:#"name"];
cell.titleLabel.text = name;
//[cell changeArrowWithUp:([indexPath isEqual:indexPath]?YES:NO)];
if ([indexPath isEqual:indexPath])
{
[cell changeArrowWithUp:YES];
}
else
{
[cell changeArrowWithUp:NO];
}
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:self.selectIndex];
[cell changeArrowWithUp:YES];
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:self.selectIndex];
[cell changeArrowWithUp:NO];
}else
{
NSIndexPath *tempIndex=indexPath;
if (tempIndex)
{
Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:tempIndex];
[cell changeArrowWithUp:NO];
}
Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:self.selectIndex];
[cell changeArrowWithUp:YES];
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}else
{
NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:#"list"];
NSString *item = [list objectAtIndex:indexPath.row-1];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"sample tree" message:item delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
// NSArray* temp=[NSArray arrayWithObjects:one,second,third,nil];
// [self viewPresent:[temp objectAtIndex:indexPath.row-1] sectionAt:item];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//method for pushing view controller
-(void)viewPresent:(UIViewController *)viewName sectionAt:(NSString *)sectionValue
{
[[self navigationController]pushViewController:viewName animated:YES];
}
- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert
{
self.isOpen = firstDoInsert;
[self.expansionTableView beginUpdates];
int section = self.selectIndex.section;
int contentCount = [[[dataList objectAtIndex:section] objectForKey:#"list"] count];
NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
for (NSUInteger i = 1; i < contentCount + 1; i++) {
NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section];
[rowToInsert addObject:indexPathToInsert];
}
if (firstDoInsert)
{ [self.expansionTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[self.expansionTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
[rowToInsert release];
[self.expansionTableView endUpdates];
if (nextDoInsert) {
self.isOpen = YES;
self.selectIndex = [self.expansionTableView indexPathForSelectedRow];
[self didSelectCellRowFirstDo:YES nextDo:NO];
}
// if (self.isOpen) [self.expansionTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
#end
then create two cell with subclass of UITableViewCell
the first cell should be
.h file
#import <UIKit/UIKit.h>
#interface Cell1 : UITableViewCell
#property (nonatomic,retain)IBOutlet UILabel *titleLabel;
#property (nonatomic,retain)IBOutlet UIImageView *arrowImageView;
- (void)indication:(BOOL)up;
#end
.m file
#import "Cell1.h"
#implementation Cell1
#synthesize titleLabel,arrowImageView;
- (void)dealloc
{
self.titleLabel = nil;
self.arrowImageView = nil;
[super dealloc];
}
- (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
}
- (void)indication:(BOOL)up
{
if (up) {
self.arrowImageView.image = [UIImage imageNamed:#"UpAccessory.png"];
}else
{
self.arrowImageView.image = [UIImage imageNamed:#"DownAccessory.png"];
}
}
the second cell should be
.h file
#import <UIKit/UIKit.h>
#interface Cell2 : UITableViewCell
#property (nonatomic,retain)IBOutlet UILabel *titleLabel;
#end
.m file
#import "Cell2.h"
#implementation Cell2
#synthesize titleLabel;
- (void)dealloc
{
self.titleLabel = nil;
[super dealloc];
}
- (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
}
for learning purpose my values are like below
(
{
list = (
1,
2,
3
);
name = Numbers;
},
{
list = (
a,
b,
c
);
name = Alaphabets;
},
{
list = (
India,
China,
Australia
);
name = Countries;
}
)
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.
I am having issues with my UISearchBar that I have implemented. My app will compile and run, but as soon as I hit the search bar to begin typing a word in, it crashes. Can anyone tell me if you see anything wrong with my syntax? It is suppose to go through the states that I have in my NSArray.
TableViewController.h:
#import <UIKit/UIKit.h>
#interface TableViewController : UITableViewController <UISearchBarDelegate>
#property (nonatomic, strong) NSMutableArray *results;
#property (nonatomic, strong) IBOutlet UISearchBar *searchBar;
#end
TableViewController.m:
#import "TableViewController.h"
#import "ViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
{
NSArray *states;
}
- (NSMutableArray *)results
{
if (!_results)
{
_results = [[NSMutableArray alloc] init];
}
return _results;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
states = [NSArray arrayWithObjects:#"Alabama", #"Georgia", #"Tennessee", #"Colorado", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchThroughData
{
self.results = nil;
NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:#"SELF contains [search] $#", self.searchBar.text];
self.results = [[states filteredArrayUsingPredicate:resultsPredicate] mutableCopy];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchThroughData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.tableView)
{
return [states count];
}
else
{
[self searchThroughData];
return self.results.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(tableView == self.tableView)
{
cell.textLabel.text = [states objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = self.results[indexPath.row];
}
return cell;
}
I am fairly new to Objective-C, so sorry if this is a simple question that I should know the answer to.
Thanks
The first line of your - (void)searchThroughData method sets the NSMutableArray to nil. This may be the problem.
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.
I have an app that populates a table and pushes a web view when cell in selected. I have two problems
The web view pushs and loads however the web address that loads is incorrect. i.e first cell should load web address one but it appears to be random. The web addresses come from an array.
The web view does not push on the first cell selected every time the app is launched.
Heres my code. Any help would be appriecated.
Table view
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[self setupArray];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)setupArray {
webAddress = [[NSMutableDictionary alloc]init];
[webAddress setObject:#"http://www.google.com.au" forKey:#"one"];
[webAddress setObject:#"http://www.finddrinkdine.com.au" forKey:#"Two"];
[webAddress setObject:#"http://www.kingsgroversl.com.au" forKey:#""three"];
[webAddress setObject:#"http://www.yahoo.com.au" forKey:#"Four"];
[webAddress setObject:#"http://www.scu.edu.au" forKey:#"Five"];
[webAddress setObject:#"http://www.whitenow.com.au" forKey:#"Six"];
menuItems = [webAddress allKeys];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [webAddress count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [menuItems objectAtIndex:indexPath.row];
return cell;
}
Detail View
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return self;
}
-(void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[viewWeb loadRequest:req];
[super viewWillAppear:animated];
}
[webAddress allKeys]; will return NSArray but ordering is not maintained in Dictionaries and hence it is returning random array.
menuItems = [webAddress allKeys];
replace above codes with sorted array or
menuItems = #[#"one", #"two",...];