didSelectRowAtIndex issue - iphone

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

Related

Populating UITableView row 0 static and the rest with NSArray

In one section of my app I have a UITableView which is working fine right now. I would like to set row 0 cell.textLabel.text to #"Some string". Once row 0 has been set I would then like to load the rest of the rows from an array. Currently on load my array populates the table view but I'm trying to set row 0 as a sticky. The closest example I can think of is a forum topic that is set to stay at the top. My array is constructed of returned data from a web service call.
It's been a while since I've messed with table views, and I'm having a blank on this one.
The table view is 1 section, and I get the rows by counting the elements in the array. Since I would like to create an additional cell (row 0) I would call [array count] + 1. I don't know if this approach is the best one which is why I'm reaching out to the community here.
Any insight or a shove in the right direction would be great at this point.
You're on the right track:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count]+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if ([indexPath row] == 0) {
// Code for first
[[cell textLabel] setText:#"First cell"];
} else {
[[cell textLabel] setText:[array objectAtIndex:[indexPath row]-1]];
}
return cell;
}
If you want the top of your table to be "sticky", why not consider using that string as a section header or title? In this case, the header stays visible at all times until the next section (e.g. if you had two sections, that is) is fully on the screen.
In any event, in one of my current projects I'm required to do roughly the same thing that you're doing and I have a static string being returned in row 0 (which scrolls off the top of screen when the table view scrolls down).
And in my UITableViewDataSource method, I always add one for the static cell to the number of objects in my array and in my "cellForRowAtIndexPath:" method, I increment the row by one when the indexPath.row is not zero. And if it is zero, I return my static string.
And dark_knight provides some nice sample code that illustrates what I was describing to you. So +1 to him/her.

UITableView row count in didSelectRowatIndexPath

how can i get UITableView row count in didSelectRowatIndexPath.can any one tell me a good way to get it
If you use something like this:
NSLog(#"%i", [indexPath row]);
and you should get the number of the row selected returned in the console. Alternatively you could pass [indexPath row] into a variable and use it inside a conditional statement to perform a different action depending on row selected like this:
int rowSelected = [indexPath row];
if(row == 0)
{
//do something
}
else if(row == 1)
{
//do something else
}
Hope this helps
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];----->this is the row count You have to use in didselect row method
}
a=indexPath.row;
the a variable has the value you want
You need to track the number of records passed in numberOfRowsInSection for all sections. If its varying you can keep a variable that stores the number.

Tableview with 2 section

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

How to get the indexPath of the particular table from multiple table in iphone

Hi i m using 2 tables in a single view and want to get the indexpath of the tables. I use
the below code to get the indexpath. but stil categoryId getting effected whatever table i access not the other thing. Please help...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView=categoryTblView)
{
categoryId=indexPath.row;
NSLog(#"categoryId=%d",categoryId);
}
else if(tableView=regionTblView)
{
regionId=indexPath.row;
NSLog(#"regionId=%d",regionId);
}
}
To test equality here, you need to use == instead of a single equal sign (which is for assignment).
So the code should be:
if (tableView == categoryTblView)
{
...
}
else if (tableView == regionTblView)
{
...
}

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