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;
}
Related
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'm trying to have table cells with different names, but so far they all have the text '1'.
How do I make the cells each have a unique name, as described in the tableData array?
- (void)viewDidLoad {
[super viewDidLoad];
// self.clearsSelectionOnViewWillAppear = NO;
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
tableData = [NSMutableArray arrayWithObjects:#"1", #"2", #"333", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"subTable"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL"];
}
cell.textLabel.text = [tableData objectAtIndex:0];
return cell;
}
I heard that I might need to use a method 'forIndex' or 'indexPath' or some such?
Change this line:
cell.textLabel.text = [tableData objectAtIndex:0];
to this:
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
Not to be rude, just a thought: if this is not obvious to you, you should probably start with some beginner level tutorials.
what i am trying to do is use the selection from a pickerview to populate a tableview on another view controller. The user makes a selection on the picker view then taps a button to go to the next view controller which displays different arrays in a tableview depending on the pickerview selection.
I am quite new to coding so i don't know if my code is even remotely close to being correct. Anyway heres what I've tried:
#interface HomeViewController ()
#end
#implementation HomeViewController{
}
#synthesize tableview;
- (void)viewDidLoad
{
[super viewDidLoad];
_pickerarray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3", nil];
_array1 = [[NSArray alloc] initWithObjects:#"1980",#"1981",#"1982", nil];
_array2 = [[NSArray alloc] initWithObjects:#"1983",#"1984",#"1985", nil];
_array3 = [[NSArray alloc] initWithObjects:#"1986",#"1987",#"1988", nil];
}
- (void)didReceiveMemoryWarning
{
[self setPicker:nil];
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerarray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_pickerarray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (row) {
case 0:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_array1 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 = [_array1 objectAtIndex:indexPath.row];
return cell;
}
break;
case 1:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_array2 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 = [_array2 objectAtIndex:indexPath.row];
return cell;
}
break;
case 2:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_array3 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 = [_array3 objectAtIndex:indexPath.row];
return cell;
}
default:
break;
}
}
#end
On the line of code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
I get a semantic issue stating " Use of undeclared identifier 'tableview'"
I would really appreciate any feedback, as I said I am quite new and i don't even know if I'm going about this the correct way so if anyone could steer me in the right direction that would be great!
As a novice I'd recommend you use a tableview framework such as the free Sensible TableView framework. These frameworks will enable to easily achieve such tasks almost out of box. Saves me tons of time in my projects.
In my app, I have UITableView in which first cell & last three cell's contents are fixed. But in between those cell's contents are different as per array count & content.
E.g: 1st cell+ array count + last three cells.
The array count different all times.
How can make the table view as I explaining above.
//Working code for 1st cell+[dynamic array]+last three Fixed cells
#import "tableTOViewController.h"
#interface tableTOViewController ()
#end
#implementation tableTOViewController
- (void)viewDidLoad
{
myArray=[[NSArray alloc] init];
myArray=[NSArray arrayWithObjects:#"Aman",#"Atul",#"Karan",#"Yen",#"Chan",#"Lee", nil];
lastFixedArray=[[NSArray alloc] init];
lastFixedArray=[NSArray arrayWithObjects:#"Snakes",#"Food",#"Drink", nil];
tblView.delegate=self;
tblView.dataSource=self;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myArray count]+4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=#"cellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.row==0)
cell.textLabel.text=#"First Cell";
else if (indexPath.row>[myArray count])
cell.textLabel.text=[lastFixedArray objectAtIndex:([indexPath row]-[myArray count]-1)];
else
cell.textLabel.text=[myArray objectAtIndex:indexPath.row-1];
return cell;
}
#end
If you want to add subviews to last three cells then you can use following condition
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=#"cellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.row==0)
cell.textLabel.text=#"First Cell";
else if (indexPath.row>[myArray count])
{
int k=([indexPath row]-[myArray count]-1);
switch (k) {
case 0:
[cell.contentView addSubview:thirdLastView];
break;
case 1:
[cell.contentView addSubview:secondLastView];
break;
case 2:
[cell.contentView addSubview:lastView];
break;
}
cell.textLabel.text=[lastFixedArray objectAtIndex:k];
}
else
cell.textLabel.text=[myArray objectAtIndex:indexPath.row-1];
return cell;
}
In your numberOfRows method, return value as
4 + [array count];
In cellForRowAtIndexPath do a switch on indexPath.row and accordingly fill your cell contents.