Multiple selection to array. - objective-c

My problem is that I load players from database to UITableView and i want to store this players in Array but I provide multiple selection. Next I want so save this information to DB. I have done DB layer so just need information how to store this multiple selected players to array.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.cellSelected = [NSMutableArray array];
self.selectedItems = [NSMutableArray array];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Cell Initialisation here
if ([self.cellSelected containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below
//self.selectedIndexPath = indexPath;
//the below code will allow multiple selection
if ([self.cellSelected containsObject:indexPath])
{
[self.cellSelected removeObject:indexPath];
[self.selectedItems removeObject:[self.selectedItems indexOfObject:[self.dataArray objectAtIndex:indexPath.row]]];
}
else
{
[self.cellSelected addObject:indexPath];
[self.selectedItems addObject:[self.dataArray objectAtIndex:indexPath.row]];
}
[tableView reloadData];
}
Where your data array hold data from database (Whole players information). selectedItems array contains the details of selected player info.
Hope this helps :)

Related

How to show in a UILabel what you Selected in a UITableViewCell (didSelectedRowAtIndexPath)

I am coding a registration form where users must enter their country. I listed the countries through a .plist file in a UITableView (Classe ViewController). In addition , I set the single selection with the checkmark. After selected the country the APP must go back and show in the country label the selected row (like a normal reg form).
Here whats under didSelectRowAtIndePath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Uncheck the previous checked row
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
Thanks for helping me.
Richard
If you have your label and your table in the same controller you can do this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.yourLabel setText:[self.arrayCountries objectAtIndex:indexPath.row]];
}
but if you have your table and your label in differents controllers you can use NSUserDefaults for keep your selection like this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[NSUserDefaults standardUserDefaults]setObject:[self.arrayCountries objectAtIndex:indexPath.row] forKey:#"selectedCountry"];
}
and in your controller where you have your label get this selection
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.labelCountry setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"selectedCountry"]];
}
As I understood, you should use delegates. Once the country selected, call a delegate function in previous form and set the value of cell text.
e.g., (untested code to give an idea)
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
// Trigger previous form to get the current form's data
// Here self.delegate takes address of previous form
[self.delegate setThisCountryNameToOwnerFormWithText:cell.titleLabel.text];
// Probably go back like this
// [self.navigationController popViewControllerAnimated:YES];
}
Just add that line like this
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
country.text=uncheckCell.label.text;//add this wherever you want, but it should be in this method only
}
where country is the label you created.
Also this line of code will work only if your label that is country and your table where you select the row both are in the same class,could you mention how you have made your classes like where your UITableView is?and where your UILabel is?

TableView not reloading properly

I am making an settings screen in which you can select stations via a uisearchbar. I have a sectioned tableview, with the first letter of a station as the header and every station is categorized by it's first letter. So far so good.
I habe 2 NSMutableArray's with, per section, the stations. One is the unfiltered array (Which I use when I don't have it filtered) and the other one, when I am searching for something. (I do this via a predicate). On every keypress on the keyboard I do a [self.tableView reloadData]; this works, HOWEVER the scrollview stays too long! So you can scroll way past how many results are actually in the selected array. This causes a crash, because it's trying to get objects that don't exist.
So it seems like the tableview isn't counting the array right or something?
Is anyone familiar with this problem?
Here is some code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.searching) {
return [self.tableFilterd count];
} else {
return [self.tableData count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Rows for section");
// Return the number of rows in the section.
if (self.searching) {
NSLog(#"Editing section: %i, count %i", section, [[self.tableFilterd objectAtIndex:section] count]);
return [[self.tableFilterd objectAtIndex:section] count];
} else {
NSLog(#"Not editing");
return [[self.tableData objectAtIndex:section] count];
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SettingsHeaderCell *cell = [[[SettingsHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"HeaderCell"] autorelease];
cell.labelLetter.text = [[self.tableLetters objectAtIndex:section] capitalizedString];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 52;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
SettingsCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[SettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (self.searching) {
StationObject *object = (StationObject *)[[self.tableFilterd objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell setStationObject:object];
} else {
StationObject *object = (StationObject *)[[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell setStationObject:object];
}
return cell;
}
You might have solved this by now but I suspect you aren't emptying either arrays. In the method:
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
//Remove all objects first
[self.tableFiltered removeAllObjects];
[self.tableData removeAllObjects];
Also you only need to call [self.tableView reloadData]; in textDidChange, not in the other three methods. Hope this helps.

How to keep selection of untouched cells intact?

I have a tableView where I select cells and add the data in an array, I also have the option of swiping and then deleting a particular cell which eventually deletes the data from the array.
The problem is that once I delete a row, I lose all my selection state after I reload the table,
For that I checked again with the selection array and reselected all these cells,
BUT I am stuck at one place, Much before I actually delete a cell and reload the tableView, as soon as I swipe over a cell, selection state of all other cells also go away.
NOTE: I have two arrays, one with list of itmes to be displayed in the tableView and one with the selected items.
Here is some code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.contactList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell.textLabel setTextColor:[UIColor colorWithRed:103.0/255.0 green:103.0/255.0 blue:103.0/255.0 alpha:1.0]];
[cell.textLabel setFont:[UIFont fontWithName:#"ITCAvantGardeStd-Bk" size:14.0]];
if (![[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"nickName"] isEqualToString:#""])
cell.textLabel.text = [NSString stringWithFormat:#"%#",[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"nickName"]];
else
cell.textLabel.text = [NSString stringWithFormat:#"%# %#",[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"firstName"],[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"lastName"]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected cell index==>%d\n",indexPath.row);
//NSString *emailID = [NSString stringWithFormat:#"%#",[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"email_key"]];
NSLog(#"emailID==>%#\n",[self.contactList objectAtIndex:indexPath.row]);
[self.emailShareList addObject:[self.contactList objectAtIndex:indexPath.row]];
//[self.emailShareList insertObject:emailID atIndex:indexPath.row];
NSLog(#"Array value==>%#\n",self.emailShareList);
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"deSelected cell index==>%d\n",indexPath.row);
NSString *emailID = [NSString stringWithFormat:#"%#",[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"email_key"]];
NSLog(#"emailID==>%#\n",emailID);
[self.emailShareList removeObject:[self.contactList objectAtIndex:indexPath.row]];
NSLog(#"deSelect row Array value==>%#\n",self.emailShareList);
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
if(indexPath.row != 0)
{
NSString *contactID = [[self.contactList objectAtIndex:indexPath.row] objectForKey:#"contactId"];
NSLog(#"content on delete row==>%#\n",contactID);
[self.contactList removeObjectAtIndex:indexPath.row];
[self deleteContactToServer:contactID];
}
}
[contactTableView reloadData];
for (int i = 0; i < [self.emailShareList count]; i++)
{
for (int j = 0; j < [self.contactList count]; j++)
{
if([[[self.contactList objectAtIndex:j] valueForKey:#"email"] isEqualToString: [[self.emailShareList objectAtIndex:i] valueForKey:#"email"]])
{
NSIndexPath *path1 = [NSIndexPath indexPathForRow:j inSection:0];
[contactTableView selectRowAtIndexPath:path1 animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellEditingStyle style = UITableViewCellEditingStyleNone;
if(indexPath.row != 0)
style = UITableViewCellEditingStyleDelete;
return style;
}
When you delete an item, you don't necessary have to reload the entire tableview. You could use the – deleteRowsAtIndexPaths:withRowAnimation: method to just remove the cell in question (along with an according model update). This will probably retain your selection.
To keep your selections when entering editing mode (swipe for delete is a special case of editing mode as well) you nee to do two things:
First, enable allowsSelectionDuringEditing on your tableView:
self.tableView.allowsSelectionDuringEditing = YES;
Second, create a UITableView subclass and override setEditing:animated: like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSArray *indexPaths = self.indexPathsForSelectedRows;
[super setEditing:editing animated:animated];
for (NSIndexPath *ip in indexPaths) {
[self selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
Personally, I would rather use some sort of custom selection mechanism, when selections are important from a model point of view. I would create a custom cell subclass, add a selection property to it let it change the cell styling accordingly. The build-in features that affect regular table view selections won't cause problems with such an approach.
Here is an additional method of preserving table selections in and out of edit mode without having to subclass UITableView. Add the following to your UITableViewControllerView.
Within viewDidLoad add:
self.tableView.allowsSelectionDuringEditing = YES;
Then override setEditing:animated:
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
[super setEditing:editing animated:animate];
for (NSIndexPath *selectedIndexPath in selectedIndexPaths) {
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}

Initialize UITableView with dynamic data

I am trying to display a list of friends in a UITableView.
I am loading the friends:
- (void)viewDidLoad
{
[super viewDidLoad];
[self apiGraphFriends];
}
Then I am setting my results in:
- (void)request:(FBRequest *)request didLoad:(id)result
{
friends = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *resultData = [result objectForKey:#"data"];
if ([resultData count] > 0) {
for (NSUInteger i=0; i<[resultData count]; i++) {
[friends addObject:[resultData objectAtIndex:i]];
}
} else {
//[self showMessage:#"You have no friends."];
}
}
and I am implementing the required UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [friends count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
//NSManagedObjectModel *friend [fetch
FriendCell *cell = [tableView dequeueReusableCellWithIdentifier: #"friendCell"];
cell.cellName.text = [friends objectAtIndex:indexPath.row];
return cell;
}
The problem is that the method 'cellForRowAtIndexPath' is being called before my data has arrived, how do you prevent the automatic initialization of the table view and only initialyse it when you have data for it ?
The tableview is always loaded on launch. Instead, after the data has finished loading, call
[self.tableView reloadData];
This tells the tableView to refresh, and it calls cellForRowAtIndexPath and all that jazz again.
how do you prevent the automatic initialization of the table view
You don't. The table view will automatically try to load its data when it is created and displayed. But that should not be a problem.
and only initialyse it when you have data for it.
Send a reloadData message to the table view once the data is ready.
Have you remembered to change the number of sections from 0 to at least 1?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
To fully test this you can use the following code
- (void)viewDidLoad
{
[super viewDidLoad];
//Add your friend as you initialise the array
NSMutableArray *array = [NSMutableArray arrayWithObjects:f1,nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.friends 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];
}
//change this to whatever you need
NSString *friend = [self.friends objectAtIndex:indexPath.row];
cell.textLabel.text = friend;
return cell;
}
Your numberOfRowsInSection should return 0 if you don't have anything to display. After the data is loaded and ready to display, you should call reloadData on the tableView, and only then should the data be displayed. numberOfRowsInSection should now give the number of rows you loaded.
In short, you don't prevent the tableView from initializing. You initially tell it to display no data, and once your data is loaded, you tell it to display as many rows as you have.
Don't set the tableview delegate and datasource until AFTER you have your data.
i.e.
-(void)viewDidLoad{
[self apiGraphFriends];
self.tableview.delegate = self;
self.tableview.datasource = self;
}
I am assuming your method 'apiGraphFriends' doesn't use any background threading and isn't asynchronous. If it is, then just create a new method and put the datasource/delegate setting in there, then call it from a block in your apiGraphFriends method.
simply call reloadData of the tableView once you got your data, the tableview will reload its data and thus recall all your datasource methods to get its data.
You are so close!
- (void)viewDidLoad
{
[self apiGraphFriends];
[super viewDidLoad];
}
That should do it. If not, you can always call reloadData on the tableView.

Need help adding array to a tableview

I'm having trouble adding cells with different content to my app. I know (or think) this needs to be done using an array, but I'm new to xcode and objective-c so I'm not exactly sure how to do this. Any help is much appreciated. Thanks.
Here is a copy of my current code:
#import "RootViewController.h"
#import "CustomCell.h"
#implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageViews.image = [UIImage imageNamed:#"captainaq_4.jpg"];
cell.firstLabel.text = #"name ";
cell.secondLabel.text = #"second name";
cell.thirdLabel.text = #"third name";
cell.thirdLabel.textAlignment = UITextAlignmentCenter;
// Configure the cell.
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
}
#end
First, note that cellForRowAtIndexPath is being called multiple times-- once for each cell. I think your best option would be to declare an array, and populate it with the objects you want displayed, as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *days[] = {#"Mon", #"Tues", #"Wed", #"Thurs", #"Fri", #"Sat", #"Sun"};
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText:days[indexPath.row]];
return cell;
}
Or populate the array somewhere else, if you need to manipulate the objects within it. In that case (assuming your array is populated with strings), your call to setText would look like:
[[cell textLabel] setText:[yourArray objectAtIndex:indexPath.row]];
It also appears you want to have a header with an image in it. In that case, use:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//create your UIImageView and add your image
return yourImageView;
}
ok... as far as I have seen , I dont think the following is valid.
cell.imageViews.image = [UIImage imageNamed:#"captainaq_4.jpg"];
cell.firstLabel.text = #"name ";
cell.secondLabel.text = #"second name";
cell.thirdLabel.text = #"third name";
cell.thirdLabel.textAlignment = UITextAlignmentCenter;
Correct me if I am wrong...
As far as I understood your question, what you got to do is that create labels and add them either programmatically or through Interface Builder ( whichever is easier for you) so that you can display whatever contents you want in the cell...
Here is an example of what you can do..
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // adding indicator to the rows
}
// Configure the cell...
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"John Doe";
cell.detailTextLabel.text = #"DEPT";
//cell.imageView.image = [UIImage imageNamed:#"meeting_color.png"];
break;
case 1:
cell.textLabel.text = #"Mary Smith";
cell.detailTextLabel.text = #"DEPT";
//cell.myImageView.image = [UIImage imageNamed:#"call_color.png"];
break;
case 2:
cell.textLabel.text = #"Bob Wong";
cell.detailTextLabel.text = #"DEPT";
//cell.myImageView.image = [UIImage imageNamed:#"calendar_color.png"];
break;
default:
break;
}
return cell;
}
Again that is a very simple way you can add content to a tableview...
You can create several different UITableViewCell or your own custom cell class and add it into array and display it later under:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
With this, you can have several distinct custom cell (What I mean distinct is totally different cell, such as first cell with image while the other doesn't have image).
While the other alternative is to do it inside the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
as shown by using switch case. Either ways, it's about preference.
I suggest you to learn more about the basic use of classes, such as NSArray, NSMutableArray, and writing Objective-C language first. While it doesn't help much with learning developing app, it will speed up the learning curve during the real implementation of building program in Cocoa environment.