How to setup cells in a static table divided in sections programmatically - ios

I'm basically making a settings view on my app and I'm using a static table for it. So I divided the table into 3 sections each with one cell. Not programmatically I can easily label each cell and it works but programmatically I'm not able to initialize each cell. I can only initialize the first cell that gets repeated across the 3 sections. I would like a way to initialize a cell for each section but I can't find a method or a way to do that. My tableview also has reuseIdentifiers but it doesn't seem like the UITableViewController recognizes it.
This is what I have done so far.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EditProfile";
//UITableViewCell *cell = nil; //[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// More initializations if needed.
}
//Observations *currentList = [self.teacherNames objectAtIndex:indexPath.row];
cell.textLabel.text = #"Hey";
//return cell;
return cell;
}
But what I want is the cell in the first section to be labeled: Edit Profile, the second one: Invite and the third:Logout

You have to make a condition for how to handle each different section within the table view.
if (indexPath.section == 0) {
cell.textLabel.text = #"Edit Profile";
}else if (indexPath.section == 1) {
cell.textLabel.text = #"Invite";
}else if (indexPath.section == 2) {
cell.textLabel.text = #"Log out";
}......

Check out the tableView delegate methods:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
(IF you're wanting to label the section headers and not the cells in the sections themselves)

Related

Single Array Value to show multiple tableview on one viewController

I am developing Iphone Application.Using Three table view on Single viewcontroller. value show on those tableview's on single array but problem is that those array value show on first tableview not on other two tableview's. please help Thanks in advance
code..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_arrayResult 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];
}
MEObject *obj = [_arrayResult objectAtIndex:indexPath.row];
if (tableView == _tableView) {
cell.textLabel.text= obj.emp_client_name;
}
if (tableView == _secondTableView) {
cell.textLabel.text= obj.emp_event_name;
}
if (tableView == _thirdTableView) {
cell.textLabel.text=obj.emp_client_name;
}
return cell;
}
Please cross check if you are setting datasource and delegates of all 3 tables to viewcontroller.
When you set your _arrayResult, you need to reload all 3 table view
_arrayResult = #[a,b,c];
[table1 reloadData];
[table2 reloadData];
[table3 reloadData];
After once you set the datasource/Delegate to the all three tableview. I haven't seen any datasource allocation as per your tableview. Like
if tableview == tableview1 {
Return first array
}
Can you please try giving name to those tableview on outlet and then for each datasource and delegates function call as per your requirement but before check which tableview is displayed.

How to save data from a textfield to a tableview?

I'm working on my first app and I'm kind of new to Objective-C, I want to make it so when someone types in a text field then presses a button it will save it to a table view. Does anyone know how to do this or know any tutorials.
all you have to do is everytime you press the button you'll refresh/update your tableview.
- (IBAction)buttonPressed:(id)sender {
NSString *textNameToAdd = yourTextField.text;
[containerArrayForTableView addObject:textNameToAdd];
[myTableView reloadData];
}
// UITABLEVIEW DELEGATES
- (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.
// Usually the number of items in your array (the one that holds your list)
return [containerArrayForTableView count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell... setting the text of our cell's label
cell.textLabel.text = [containerArrayForTableView objectAtIndex:indexPath.row];
return cell;
}
refer here if you are having trouble configuring with UITableView.
These are some good tutorials for you..
http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/
http://www.codigator.com/tutorials/ios-uitableview-tutorial-for-beginners-part-1/
http://iosmadesimple.blogspot.in/2012/10/adding-uitableview-tutorial.html
Happy coding.
EDIT:
Here what i made for you:
Simple Demo With TextField & TableView
EDIT 2:
Simple Demo With 2 TextFields & 2 TableViews

UITableView Cells With DIfferent Format & Behavior in Same Table Section

I have a UITableView with one section. All of the cells in that one section have cells that are derived from a subclass of UITableViewCell called PLContactCell.
What I'd like to do is, for the very last row of the table only, not use a PLContactCell. I just want to use a normal UITableViewCell that I can format however I would like. I'd also like to be able to have this cell NOT respond to being tapped.
My initial cellForRowAtIndexPath method is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PLContactCell *cell = [tableView dequeueReusableCellWithIdentifier:[PLContactCell reuseIdentifier]];
if (!cell) {
cell = [PLContactCell reusableCell];
cell.delegate = self;
}
id modelObject = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
if ([modelObject isKindOfClass:[NSString class]]) {
[cell configureWithString:modelObject];
} else {
[cell configureWithUser:modelObject];
}
return cell;
}
EDIT
So I tried created a UITableView cell in the XIB file and added the reuse identifier of "newCell" to it. Then I added this code:
if (indexPath.row == [[sections objectAtIndex:indexPath.section] count] - 1) {
NSString *CellIdentifier = #"newCell";
noFormatCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
This doesn't do anything. My question is, how do I access the last row of the section and how do I make it so that that cell it is not a PLContactCell but a UITableView Cell.
If it's always at the end, you might consider using the footer view of the UITableView. You can then keep some extra logic out of your UITableViewDataSource.
If it HAS to be as a cell, you'd have to add an extra row count on your last section, then do an if statement check to watch out for it in your -tableView:cellForRowAtIndexPath: implementation. I would strongly urge you try the footer approach, as it's cleaner and way easier to figure out what you were doing a few months/years from now.
Here's some code. Note you'd need to make another section if you are using grouped style in the UITableView.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == sections.count - 1) //Looking for last section
{
return [sections objectAtIndex:section].count + 1; //Add one to the last section
}
return [sections objectAtIndex:section].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = indexPath.row;
if ((sections.count == indexPath.section) && [sections objectAtIndex:indexPath.section].count == indexPath.row)
{
//Here you would create and return your UITableViewCell
}
PLContactCell *cell = [tableView dequeueReusableCellWithIdentifier:[PLContactCell reuseIdentifier]];
if (!cell) {
cell = [PLContactCell reusableCell];
cell.delegate = self;
}
id modelObject = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
if ([modelObject isKindOfClass:[NSString class]]) {
[cell configureWithString:modelObject];
} else {
[cell configureWithUser:modelObject];
}
return cell;
}

UITableview grouped style only showing one value from array?

MY UITableview Group style only showing one value from the array of 4 elements.
in viewdidload i add following array
_displayThemeItems=[[NSArray alloc]initWithObjects:#"Default",#"Night",#"Sepia" ,#"Blue" ,nil];
_displayThemeIcons=[[NSArray alloc]initWithObjects:[UIImage imageNamed:#"day.png"],[UIImage imageNamed:#"night.png"],[UIImage imageNamed:#"sepia.png"], [UIImage imageNamed:#"blue.png"],nil];
and my table delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_displayThemeItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// No cell available - create one.
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];}
NSLog(#"rray%#",_displayThemeItems);
cell.textLabel.text = [_displayThemeItems objectAtIndex:indexPath.row];
cell.imageView.image=[_displayThemeIcons objectAtIndex:indexPath.row];
return cell;
}
Your code look fine to me, the only the only reason that I can think amy effect the result you see is how you set up your table view.
Make sure that your table view frame is bug enough to show the 4 items, and make sure you set the tabe view data source.
Add the "}" just before the return such that the code where you set the title and image comes under the "if" statement.

IOS: tableview delegate methods for two tableview

I have these delegate method for a tableview inside a class:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ;
}
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
return cell;
}
if I have a single UITableView it's ok but if I have two UITableView? How Can I organize my code? with tag?
See how all the delegate methods have a tableView:(UITableView *)tableView in them?
You can define your table views in the header file and then just simply go: (assuming your table is called myTable)
if (tableView == myTable)
Then you can have as many table views as you like.
So for example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array1 count];
}
Becomes:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == myTable)
{
return [array1 count];
}
if (tableView == myTable2)
{
return [array2 count];
}
return 0;
}
My suggestion is having your data source act as a table view delegate, instead of your controller.
This is a design more closer to the Model-View-Controller pattern and will allow you much more flexibility and avoid checking for the specific value that the tableView argument has got in your delegate methods.
In your case, your delegate/data source would be a class with a member of type NSArray and also implementing the UITableViewDelegate protocol.
Yes you can do it with tag. Give your UITableViews the tags 1 and 2.
set up an switch:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ;
}
switch ([tableView tag]) {
case 1:{
[[cell textLabel] setText:#"First tag"]
break;
}
case 2:{
[[cell textLabel] setText:#"Second tag"]
break;
}
default:
break;
}
return cell;
}
Each of those methods passes in a reference to the table view that's calling it. I usually connect each table to an outlet in interface builder and conditionally return the datasource based on a comparison with tableView in the delegate methods and the outlet names. Doing so with a tag is also possible but messier and more open to complications when editing your view structure.

Resources