Tableview with 2 section - iphone

Hello
I am developing an app which is using a tableview with two sections(instead of radio buttons).
So I want these section to act like radio buttons. To accomplish this, I have to implement an "if" in the
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
I want to use something like: if (section == "A").
How to do that?
Also is this the right way to make the sections of a table acting like a different tables?

Hmm... that sounds like an odd set up, but irrespective, you simply use the provided indexPath to obtain the section/row.
i.e.: indexPath.section will point to the selected section and indexPath.row will point to the row.
For more information, the Table View Programming Guide for iOS is a good start as it covers all of the above in quite a bit of detail.
UPDATE
In terms of highlighting multiple cells, I'd have thought that you'd want to create your own concept of highlighting, which you'd toggle on and off when the user selects the cell.
That said, you could also mark selected cells via the UITableViewCellAccessoryCheckmark as per the existing Is it possible to configure a UITableView to allow multiple-selection? question/answer.

I don't know what you mean by "section to act like radio buttons" but to distinguish between sections in tableView:didSelectRowAtIndexPath: use indexPath.section:
if (indexPath.section == 0) {
// first section
} else if (indexPath.section == 1) {
// second section
}

you must need to use indexPath.section: to distinguish between your table sections
So your code should be like below .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
{
if (indexPath.section == 0)
{
//first section
}
else if (indexPath.section == 1)
{
//second section
}
}

Related

Collapsing and Hiding static UITableViewCells of a UITableview

I have been though a ton of examples that show how to do a collapsable tableview but I cant find something that works with static cells. I have a couple cells that I made in IB that I would like to collapse if the section header is clicked but I cant figure out or understand how to do it. If anyone can tell me or point me to an example that does that, I would be grateful.
Here is an example of just one of the cells I have that would need to collapse:
You can refer to this sample: (download the sample code)
http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html
So basically what this code does is:
You create Section in your -(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section --> Each Section has SectionHeaderView --> Which you basically will tap and expand your table view.
You will implement the two delegate methods:
-(void)sectionHeaderView:(APLSectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)sectionOpened
-(void)sectionHeaderView:(APLSectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)sectionClosed
--> This will internally call insertRowsAtIndexPaths and deleteRowsAtIndexPaths --> Which will internally call tableview:cellForRowAtIndexPath --> Now here you need to create your cell which will have address, city etc etc...
Let me know if you need more information.
I finally found a solution I was happy with thanks to this: https://github.com/xelvenone/StaticDataTableViewController
A very simple solution with the implementation:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.reloadTableViewRowAnimation = UITableViewRowAnimationMiddle;
self.deleteTableViewRowAnimation = UITableViewRowAnimationMiddle;
self.insertTableViewRowAnimation = UITableViewRowAnimationMiddle;
if (indexPath.row == 0) {
[self toggleCellVissibilityOrReload:self.cell1];
}
[self reloadDataAnimated:YES];
}

didSelectRowAtIndex issue

I have a UITableView in which I have added sections. When using didSelectRowAtIndex with indexPath.row I am not getting the correct value. Let's say I'm in section 2, then it starts the row index (indexPath.row) at 0 again and therefore I am getting the wrong index.
let's say the first array has 4 names: (0)50 street, (1)apple, (2)boy, & (3)cat. when you select anyone one it loads their correct detailview based on the index (0,1,2,3). however, when you go to search and type 'b' it narrows the table down to just (2)boy which is now in row 0. so when you select 50 street it loads boy from the original array.
Can anyone tell me how to fix this?
I realize that it is possible to get the section index by indexPath.section but I can't figure out how to use this.
You can calculate the "total row from top" by adding together the counts of the above sections, ie. int j=0;for (int i=0;i<indexPath.section;i++) {j=j+[tableView numberOfRowsInSection:i];j=j+indexPath.Row;}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) // First section
{
//Do something
}
else if(indexPath.section == 1) // Second Section
{
//Do something
}
}

Filtering UITableViewCells with animation - iPhone Development

This seems simple enough but as yet I am unable to find a solution.
Basically I have a segmented control with two options. The first is the default (and is automatically displayed on load) and when selected displays all rows in a table view. The second is a filter limiting the rows displayed. This is the exact same set-up as used on the "Recents" tab of the iPhone's Phone app that filters 'All' and 'Missed' calls.
At present I have the data loading from two different arrays. The problem is that when I swap the data there is no animation to denote that the rows have been filtered. Apple have implemented this in their Phone app but I can see no way of acheiving this.
Perhaps each cell will need to be deleted and re-added as the user switches between the two states - or perhaps setting the height of the cells that I wish to hide to 0 would acheive the same effect? Does anyone have any experience of producing this accordian-type animation?
I have looked here for some clues but am having problems rolling some code that works. Has anyone implemented this before? If so, how did you get it to work?
You can accomplish a similar effect by calling deleteRowsAtIndexPaths:withRowAnimation: and insertRowsAtIndexPaths:withRowAnimation: on your table view with a UITableViewRowAnimationFade animation.
Have you looked into reloadSections:withRowAnimation:?
The basic idea is to call reloadSections:withRowAnimation: and in your UITableViewDataSource implementation switch on the segmented control's selectedSegmentIndex.
Assuming your data is flat (only one section) it would look something like this:
- (IBAction)segmentSwitch:(id)sender
{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (self.segmentedControl.selectedSegmentIndex)
{
default:
case 0:
return [self.allRows count];
case 1:
return [self.onlySomeRows count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id data;
switch (self.segmentedControl.selectedSegmentIndex)
{
default:
case 0:
data = [self.allRows objectAtIndex:[indexPath row]];
break;
case 1:
data = [self.onlySomeRows objectAtIndex:[indexPath row]];
break;
}
//TODO: use data to populate and return a UITableViewCell...
}

iPhone UITableView with index can I push a different detailed view with every different cell?

I know its possible to create a table that has an index on the side and a search bar at the top that a user can type in to find an item, but is it possible to say to the table if array isEqual to "item1" push view1? I would like to push a different view with each cell. Anyone have any advice?
Sure. Just create the appropriate view (controller) depending on the cell's indexPath in tableView:didSelectRowAtIndexPath:.
Create the cell based on the index path. If you create all the cells ahead of time, store them in an array by row index. If you are creating them as needed, do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *result;
switch ( [indexPath row] ) {
default: result = [self tableView:tableView normalCellForRowAtIndexPath:indexPath]; break;
case 3: result = [self tableView:tableView detail3CellForRowAtIndexPath:indexPath]; break;
case 5: result = [self tableView:tableView detail5CellForRowAtIndexPath:indexPath]; break;
}
return result;
}
Check out the sample programs from the book Beginning iPhone Development. You must sign up, but its free. You should look specifically at chapter 9's sample code called Nav. Shows you exactly what you need. This was my first book on the subject and was well worth it.
http://www.iphonedevbook.com/forum/viewforum.php?sid=3010c045df967353c6b3894889e6b8f5
Cheers!
Ken

Preventing cell reordering for particular cells in iPhone SDK

I have 2 sections in my UITableView. I want the 2nd section to be movable but the 1st section of cells not.
Specifying canEditRowAtIndexPath and canMoveRowAtIndexPath doesn't help - the first section cells although not showing drag controls, they still change places if a cell from the 2nd section is dragged over.
Is there a workaround for this?
Try implementing the targetIndexPathForMoveFromRowAtIndexPath method and forcing the row from the second section back to its original place if user tries to move it to the first section:
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section == 0)
{
return sourceIndexPath;
}
else
{
return proposedDestinationIndexPath;
}
}