obj c different table cell names in a table - ios

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.

Related

How to create multiple columns in UITableView i.e CustomCellView?

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.

UITableView mixing up section items

I'm trying to open 2 different Viewcontrollers from the first 2 sections of my tableview and some URL's from the other 4 sections. The first section doesn't do anything and the others are mixing up, and the sections with which should open the URL's don't work. Thanks for your responses.
#implementation SettingsViewController {
NSArray * sectionheaderArr;
}
- (void)viewDidLoad
{
sectionheaderArr = [[NSArray alloc]initWithObjects:NSLocalizedString(#"fb",
nil),NSLocalizedString(#"recommend", nil),NSLocalizedString(#"feedback",
nil),NSLocalizedString(#"rate", nil),NSLocalizedString(#"language",
nil),NSLocalizedString(#"all_providers", nil), nil];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
return sectionheaderArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier =#"Cell";
int row = indexPath.row;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [sectionheaderArr objectAtIndex:row];
return cell;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.section == 0) {
if (indexPath.row ==0){
[self performSegueWithIdentifier:#"openFacebook" sender:self];
}
}
if (indexPath.section ==1) {
if (indexPath.row ==1) {
[self performSegueWithIdentifier:#"opentest" sender:self];
}
}
if (indexPath.section==2) {
if (indexPath.row ==2) {
UIApplication*app1 = [UIApplication sharedApplication];
NSString*path = #"http://www.google.de";
NSURL*myurl = [NSURL URLWithString:path];
[app1 openURL:myurl];
}
}
}
Here it is the answer to your question:
#implementation SettingsViewController {
NSArray * sectionheaderArr;
}
- (void)viewDidLoad
{
sectionheaderArr = [[NSArray alloc]initWithObjects:NSLocalizedString(#"fb",
nil),NSLocalizedString(#"recommend", nil),NSLocalizedString(#"feedback",
nil),NSLocalizedString(#"rate", nil),NSLocalizedString(#"language",
nil),NSLocalizedString(#"all_providers", nil), nil];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
return sectionheaderArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier =#"Cell";
int row = indexPath.row;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [sectionheaderArr objectAtIndex:row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.row ==0){
[self performSegueWithIdentifier:#"openFacebook" sender:self];
}
if (indexPath.row ==1) {
[self performSegueWithIdentifier:#"opentest" sender:self];
}
if (indexPath.row ==2) {
UIApplication*app1 = [UIApplication sharedApplication];
NSString*path = #"http://www.google.de";
NSURL*myurl = [NSURL URLWithString:path];
[app1 openURL:myurl];
}
}
Please note that I've also changed the "didSelectRowAtIndexPath" method as you were implementing "didDeselectRowAtIndexPath" so it won't work until you DEselect your cell.
Thanks also #iphonic for his explanation.
The datasource method for UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
is used to define number of sections for UITableView, and each section has different rows starts from 0,1,2.....
In your case you are trying with different indexPath.section where as it should use only 1 section, and for multiple sections there should be multiple datasource for the sections to be defined.
As you already got the solution, I just want to clear the concept of indexPaths rows and sections.
Thanks.

A uitable view with 3 groups in it

i am a developing a ios app with a view controller and a table view in it. i am trying to load list of items in 3 groups but when i compile it it shows correct count but not showing all the items jus repeating items. please help. let me post my code here.
#interface ViewController ()
#end
#implementation ViewController {
NSArray *menuItems;
NSArray *menuItems2;
NSArray *dash;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
menuItems = #[#"itm1", #"itm2", #"itm3", #"itm4"];
menuItems2 = #[#"itm1", #"itm2", #"itm3", #"itm4"];
dash = #[#"itm1", #"itm2"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0) {
return [menuItems count];
}
if (section == 1) {
return [menuItems2 count];
}
if (section == 2) {
return [dash count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
#end
Your cellForRowAtIndexPath... method needs to be written so the cell is populated with proper data based on the cell's section and row. Right now you don't do anything but use an empty cell.
You don't configure your cells. After dequeuing a cell, you have to set its title.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Get the correct array, depending on the current section.
NSArray *items = nil;
if (indexPath.section == 0) {
items = menuItems;
}
else if (indexPath.section == 1) {
items = menuItems2;
}
else if (indexPath.section == 2) {
items = dash;
}
// Get the string from the array and set the cell's title
NSString *title = items[indexPath.row];
cell.textLabel.text = title;
return cell;
}

How to select and save multiple values from tables rows

here i have made a table class in which i have a list of 120 words, now i have to select minimum ten rows of the table for further precision.. please any one can guide me that how can i select more than 10 rows from table and save these value in specific array or somewhere else.. please help me out of it.
#implementation tableview
NSArray *myArray ;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
myArray = [NSArray arrayWithObjects:
#"ad (a-d)",.......,Nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
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 = [myArray objectAtIndex:indexPath.row]; //=#"ASDF" works.
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
}
#end
For multiple cell selection in your table add this code inside didSelectRowAtIndexPath: method. For saving selection values, create an iVar NSMutableArray and add the newly selected object inside didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
One quick idea that come into mind is, as soon as you select a row/cell, save the values in a collection object (array, dictionary), and keep on adding.
Delete from the collection if you deselect the row/cell.

populating a grouped UITableView

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;
}

Resources