Here is my viewcontroller.m
#interface ViewController ()
{
NSArray *sectionTitleArray;
}
#property (strong, nonatomic) IBOutlet UITableView * tablevw;
- (void)viewDidLoad
{
[super viewDidLoad];
_tablevw.delegate = self;
_tablevw.dataSource = self;
sectionTitleArray = #[ #{#"text": #"About Step0ne"},
#{#"text": #"Profile"},
#{#"text": #"Matches"},
#{#"text": #"Messages"},
#{#"text": #"Photos"},
#{#"text": #"Settings"},
#{#"text": #"Privacy"},
#{#"text": #"Reporting issues"},
];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return sectionTitleArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.row]objectForKey:#"text"];
return cell;
}
I am new to Xcode am getting output as in every tableview cell first object i.e About Step0ne is displaying i need all objects should be displayed in UITableView cell.Any help is appreciable.
Since you have "count" sections, each with one row, you need to change this line:
cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.row]objectForKey:#"text"];
to:
cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.section]objectForKey:#"text"];
BTW - this can be written more easily as:
cell.textLabel.text = sectionTitleArray[indexPath.section][#"text"];
Or, if you really want one section with "count" rows, leave that line as-is and update your numberOfRowsInSection and numberOfSectionsInTableView accordingly.
replace bellow two methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return sectionTitleArray.count;
}
Related
Please if you are going to add the link of the documentation, please don't do that
I have a problem her and i don't know how to solve it and need a clear answer
I have build a table view from xcode UI and add 5 sections each section contains some cells except section2, what I want to do is to add cells to section2 in viewDidLoaded is that possible or not
this is the header file
#interface Menu : UITableViewController;
this is the implementation file
#implementation Menu
#synthesize drawerTableView;
- (void) viewDidLoad {
NSDictionary *object = [drawerTableView dataSource];
KhawaterDataManager *sharedManager = [KhawaterDataManager instance];
CategoriesResponse *categories = [sharedManager categories];
[categories printObject];
}
/*- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"%d", section);
switch (section) {
case 1:
return 5;
break;
}
return [tableView.dataSource[section] 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 setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
cell.textLabel.text = #"hello";
return cell;
}*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
// my logic
}
#end
I think you are using static cells in your UITableView. What you can do is make use of dynamic tableview cells. You have to uncomment and use both methods to create your cell:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
I am just starting iOS development. I am trying to display a multiple columns in UItableview from NSArray? So please help me anyone? I have created one column but i don,t know how to create another column?
I have done the following.
// This is my code
- (NSArray *)numbers
{
NSArray *col1 = #[#"1", #"2", #"3", #"4", #"5"];
return col1;
}
- (NSArray *)numberCodes
{
NSArray *col2 = #[#"Roy", #"Ankit", #"Jhon", #"Kem", #"Pawan"];
return col2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.numbers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
//cell.textLabel.text = [self.numbers objectAtIndex:indexPath.row];
cell.textLabel.text = [self.numberCodes objectAtIndex:indexPath.row];
return cell;
}
Step-1
on your viewController.h create the one Global Array like
#property (nonatomic,retain) NSMutableArray *tableData;
on your ViewController.m allocate the memory of that array like
- (void)viewDidLoad
{
[super viewDidLoad];
tableData = [[NSMutableArray alloc] initWithObjects:#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",#"Ten",nil];
}
on that tableview Datasource method do like
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"XXXX";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
for sample see this
1. You may create costume cell like having multiple fields/column in table. 2. Place multiple table view, one for each column. Use auto-layout for better look.
I am new to iOS and I am trying to build an lightweight app for practice. I am taking two textfield entries from one view controller and storing them in NSDictionary. I am transferring those data in NSArray. Now I want to display those data from NSArray to table view. I am using this line of code:
cell.textLabel.text=[self.myArray objectAtIndex:indexPath.row];
But my app is crashing when cursor comes at this line. Help is appreciated.Thanks in advance.
For UITableView you have to define at least 2 methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It seems your method numberOfRowsInSection is not implemented.
Try to add this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return [self.myArray count];
}
Of course your ViewController have to be delegated for UITableViewDataSource and UItableViewDelegate.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.yourArry count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Configure Cell
cell.textLabel.text = self.yourArry[indexPath.row];
return cell;
}
Please try this code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return [self.myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =[self.myArray objectAtIndex:indexPath.row]
return cell;
}
Trying using a pointer to an NSString (or what object you are trying to make)
NSString *string = [self.myArray objectAtIndex:indexPath.row]
Make sure you have the count of cell in the tableview set like so
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return self.myArray.count
}
Step One.
In YourViewController.h
#import <UIKit/UIKit.h>
#interface YourViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
In YourViewController.m
#import "YourViewController.h"
#interface YourViewController ()
{
UITableView * YourTableview;
}
- (void)viewDidLoad
{
YourTableview = [[UITableView alloc] initWithFrame:CGRectMake(0,0, 300, 500) style:UITableViewStylePlain];
YourTableview.delegate=self;
YourTableview.dataSource=self;
[self.view addSubview:YourTableview];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier=#"Cellid";
UITableViewCell * SoftCell=[YourTableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (!SoftCell)
{
SoftCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
textLabel.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
return SoftCell;
}
I can not connect referencing outlet to showTableView on storyboard. I can only connect to view .I was trying to show the tableview with some data. Here are the codes that I am using.Please help me.Thank you.
ViewController.h
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property(nonatomic, strong) IBOutlet UITableView *showTableView;
ViewController.m
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [jsonResults count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleIdentifier = #"SimpleIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdentifier];
}
// Configure the cell...
NSDictionary *appsdict = [jsonResults objectAtIndex:indexPath.row];
NSString *nameString = [appsdict objectForKey:#"name"];
cell.textLabel.text = [appsdict objectForKey:#"name"];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
return cell;
}
Make sure you've set your UIViewController's Custom Class to ViewController in the interface so you can properly link its outlets (including showTableView) to ViewController.
I'm trying to populate a grouped UITableView using an NSMutableArray. I want each element in the array to have its own section. i.e : One element (one row) per one section.
This is my code I have written so far.
#import "MailViewController.h"
#interface MailViewController ()
#end
#implementation MailViewController
NSMutableArray *mailboxes;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mailboxes = [[NSMutableArray alloc] initWithObjects:#"Inbox", #"Drafts", #"Sent Items", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return mailboxes.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
return cell;
}
#end
Currently this is how it looks.
How can I get this the way I described above? (first section: Inbox, second section: Drafts, Third section: Sent Items etc.) I've gotten close but not quite there yet.
Thank you.
You should change:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
to
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
These lines should go in the cellForRowAtIndexPath: method.
Your array index should be based on the sections and not the rows. Row is always fixed at zero.
Replace
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
With
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
If you want section headings as well implement the - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section and return a string based on the section..
With the code from other answers, you will be able to display one cell inside each section. What I prefer to do is have a multi-dimensional NSArray of pre-built cells and when I need them they're in index [section][row].
You currently have one array with just sections and trying to access them as rows (as other answers suggest). Your code won't work if you have more mailboxes in one table group..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [mailboxes count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
switch(indexpath.section){
case 0:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
case 1:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
case 2:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
default:
break;
}
return cell;
}