Grouped Table View - Realizing which cell was selected - ios

I am making a grouped a table view with multiple sections (using NSArrays). Anyway, normally, if I have a regular table view with NO sections I am able to use the following code to realize which cell the user tapped. This no longer works since I use NSArrays to split up the table and don't place the cell names directly into the table view code ([tblSimpleTable addObject:#"cell 1"]; <-- I don't do that). What new code should I use?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[tblSimpleTable objectAtIndex: indexPath.row] isEqual:#"iPhone"]) {

If you know the Row number and the section number
Try something like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section==yoursection)
{
if(indexPath.row==yourRow)
{
//do something
}
}
}

Related

How to Save UITableViewController cells after rearrange to NSUserDefaults

I've an iOS application developed using Objective-C & Xcode 6.4
Right know I'm working on manually rearranging the UITableViewController cells, everything working great. But after I press the bar button EDIT and the "3 underlines" appear to drag the cell anywhere I want in the UITableViewController, (( I can't save what I did )). So how could I do a Persistent save the changes done to the table cells location ?? I mean, How to save The new rearranged NSMutableArray to a Property list -NSUserDefaults-.
I'm using a Mutable Array to display the table's cells and these methods below:
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
FileWML *fileWML = [self.filesWML objectAtIndex:sourceIndexPath.row];
[self.filesWML removeObjectAtIndex:sourceIndexPath.row];
[self.filesWML insertObject:fileWML atIndex:destinationIndexPath.row];
}
PLUS would someone tell me how I can make the edit button display the word Done, while editing ?
(( I can't save what I did )) means: after I do the rearranging order I want, and then I go to the home view then I get back to the table view I edit and rearrange, the rearranging order I did get back to the default order. So, all the rearranging I did is gone.
Thanks and every help is appreciated.
You have to load your datas from your file in an array. This array will be your tableView dataSource.
When a row is moved, in the delegate method you have to change the object's place in the array. Your array should always be the same as your tableview !
Once the editing of the tableview is done you save the array in the file. The previous content in file should be erased. Then you reload your datas ([tableView reloadData].

How to Initialize my UITableView table to have a certain number of rows based on input from user and a Button

I'm really new to Objective-C here so what I'm asking may be trivial to most of you but any guidance will help.
Here's a picture of my storyboad.
My current objective is to allow for the user to enter in the number of sets (NSInteger *numReps) and then press the "Log Reps" button and have the table initialize with numReps cells that look like the prototype cell.
Now where I'm at a loss for the implementation. I've never done this kind of thing before so I'm not exactly sure what the best way to go about it is. I have thought of making a custom class for the UITableView table that would take info from the view after the Log Reps button is pushed. I'm not entirely sure how this would need to be implemented. Or can I simply add the table to the properties of the view controller and setup the table within the view controller? That was my initial idea and seems ideal so that I would have everything in one place.
Pleas advise. I am new to all of this and come from a C++ background so I'm still learning a lot of the notation.
Try this:
-(IBAction)btnLogClicked {
int iSet = 4 //Number of row in table
UITableView *tblView= [[UITab;eView alloc]initWithFrame:CGRectMake(0,50,320,100)];
tblView.delegate = self;
tblView.dataSource = self;
[self.view addSubView:tblView];
}
Table View Data Source and Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return iSet;
}
- (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];
}
// Display what you want to display in each cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
It's not clear what you want to present in your prototype cells, but you need an array (or probably an array of dictionaries) to populate the cells. The number of rows is determined by the number of entires (the count) of that array. So, if you take the number entered by the user, and add that many object to your array, then call reloadData on the table, you will get the number of rows that you want. What those object are that you add to the array, depends on what you're trying to show there.
you could start reading: Table View Programming Guide for iOS
But I can answer you:
You can add the UITableView to the UIViewController, but you need set your UIViewController like the TableView's delegate and dataSource. Your ViewController need to implement the protocol: UITableViewDataSource
The method that you are looking for is: tableView:numberOfRowsInSection:
But I really recommend you that read the Apple Reference.

How to add submenu's in table view in ios

i don no whether this question asked or not but i still searching for an answer.Am working in tableview concept and i am creating a table view like with some set of menu's like inbox, sent,setting etc.Now what i want is i want to create sub menu's inside every menu, for example if i click inbox it should show new,replied,deleted etc like this for every main menu i want to create sub menu. Using array we can load by checking section but with out using array i want to create directly and notable one i have used custom cell i also want to show images as per the menu if it is inbox i have to show inbox image. Could any one help me??
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 8;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 8;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"ViewProfileCell";
MyHomeViewCell *cell= [[MyHomeViewCell alloc] init];
cell=(MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(!cell)
{
NSArray *nibofMyHomeCell=[[NSBundle mainBundle]loadNibNamed:#"MyHomeViewCell" owner:self options:Nil];
cell=[nibofMyHomeCell objectAtIndex:0];
}
if(indexPath.section==0)
{
cell.MyHomeMenuLabel.text=#"Inbox";
}
}
Here ara a good tuts it could help you:
Expanding/Collapsing TableView Sections
Expandable UITableView
And this github awesome code here by Oliver Letterer.
You can create a UITableview which will contain cells on it as you said inbox, sent, setting etc.
After you need to create another UITableView which will contain your submenu buttons or labels like new,replied, deleted when click on inbox.
Simillarly you will do for rest of the cells in your main UITableView.
And don't get confuse on how will i identify which tableview will get called.
You will check for tableview name like below:
if(tableView==main)
{
///Code for main menu tableview.
}
else if(tableView==sub)
{
////Code for submenu tableview.
}
This you will do in all UITableView Delegate and Datasource methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
There are many ways you can do this. You could for example show each item as a section header and if the section header is tapped you show (or hide) all of the rows in that section. The rows being the 'sub menu' items (new, replied, deleted). Or you could use different sections for each part and how or hide the sections. Or show and hide rows. This is pretty much all controlled by changing the section and row counts.

What is the best way to implement one data source that services multiple table views in iOS

I have three UITableView's and they all need access to essentially the same data. The only difference is how each table view formats the data. What is the best way to implement this? I was thinking that if each table view had a tag of some sort, I could reference that from my data source when sending back the UITableViewCell's.
Each of the datasource calls passes in the UITableView that is requesting the information. Comparing this tableview to your three, you can determine which one it is and format the data differently. I would probably implement separate methods for each tableview and, pass the datasource method to the appropriate method. The other method would then create the customized cell creation and setup. eg for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == [self tableView1])
return [self tableView1:tableView cellForRowAtIndexPath:indexPath];
if (tableView == [self tableView2])
return [self tableView2:tableView cellForRowAtIndexPath:indexPath];
if (tableView == [self tableView3])
return [self tableView3:tableView cellForRowAtIndexPath:indexPath];
Assert0(NO, #"UITableView not recognized!");
return nil;
}
- (UITableViewCell *)tableView2:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do normal cell creation for tableView1 here
}
I don't know if there's any gotchas off-hand for running multiple UITableViews off of the same datasource, so tread lightly.
look into singleton design pattern - here is an example on how to implement for iOS.

Xcode Not Using NSMutableArray on Table Cells

I am building an iPhone application that is using a table cell. I set up this code to try to delete cells from the table, but it does not seem to work because I am not using a NSMutableArray. When I swipe I get the delete button but the delete button does not do anything. My question is if I am not using NSMutableArray with my table cell ho can this line: [self.dataArray removeObjectAtIndex:indexPath.row]; work?
Code Used
(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES; }
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove the deleted object from your data source.
//If your data source is an NSMutableArray, do this
[self.dataArray removeObjectAtIndex:indexPath.row];
} }
Here is the link to the apple doc's on table view programming guide. Table view programming guide It has a easy delegate method that uses NSArray and has an example code you can use for deleting and inserting. The code you have shown above is missing the delegate part in order to perform the delete. Take a look at listing 7.3 that maybe what your looking for.

Resources