I am trying to get the indexpath.section and indexpath.row for my prepareForSeague. I can pass the indexpath.row no problem however I am struggling to integrate it with sections. It passes the correct data across but not when outside the first section as I do not know how to set the sections.
So basically I need it to know which section it is in too.
I looked at objectsAtIndexes?
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
dynamicDetailTableViewController *destViewController = segue.destinationViewController;
NSLog(#" indexPath row %d",indexPath.row);
NSLog(#" indexPath section %d",indexPath.section);
// Assume self.view is the table view
destViewController.titleString = [self.namesArray objectAtIndex: indexPath.row];
//Health
destViewController.healthArray = [self.healthArray objectAtIndex:indexPath.row];
EDIT
To make it clearer, it does work however I have different sections so after the first section it doesn't pass the right value. It passes only the first Sections values correct as it is only calculating the indexpath based on the row and not the section. I need it to calculate the section too, but not sure how.
I guess your data structure is wrong, if you have multiple sections and rows. Your data structure should look something like below
Section 1
----> Row 1
----> Row 2
----> Row 3
Section 2
----> Row 1
----> Row 2
Section 3
----> Row 1
----> Row 2
----> Row 3
----> Row 4
And to fetch the values your code should be something like
[[self.array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
The above code says give me particular row for the particular section.
In your case it doesn't seems to be the case, you have got all the data in a single array, where as it should be array of array.
You are getting wrong values because for each indexPath.section your indexPath.row starts with 0, so for 1st section it works fine, but not for other sections..
Hope it helps.
Related
I am new to iOS development. I want to reload a particular set of rows for an action taken in some other row. For example: I have 2 rows in my tableview. I select an option for row1 and row2 in order. Now I go back and change the value in row1. This should automatically clear the "right detail" text of the second row as it is dependent on the first row. I did some search online and went through the apple developer docs and I see I need to use "reloadRowsAtIndexPaths" but I cannot get this to work for some reason.
Here is what I have done so far when I edit row0 when row1 already has a value:
if ([row1 length] > 0) {
NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0]; //get the index path for row1 as we need to reset its "right detail label"
NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, nil];
[self.tableViewHdl reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}
I am under the assumption this will automatically reload the cell to NULL. I would really appreciate if someone could give me an example and help me understand this concept. Thank you in advance.
I am trying to trigger some code based on if the cell I selected becomes the first or last cell on the users screen. I'm not trying to capture the index value of the data array. Just the index value of the cell that is visible on the screen. I'm sure this to create an array of visible cells.
NSArray *indexPathsForVisibleRows = [myTableView
indexPathsForVisibleRows];
But I keep hitting a dead end trying to then capture an index value based on that array.
I tried to use a CGPoint and convert that, but I keep getting an error. Any insight would be most helpful!
As per the documentation for the return value of that method:
An array of NSIndexPath objects each representing a row
index and section index that together identify a visible row in the
table view. Returns nil if no rows are visible.
The array returned from that method contains NSIndexPaths.
NSArray *indexPathsForVisibleRows = [myTableView indexPathsForVisibleRows];
for(NSIndexPath *eachIndexPath in indexPathsForVisibleRows)
{
NSInteger row = eachIndexPath.row;
NSInteger section = eachIndexPath.section;
UITableViewCell *cell = [myTableView cellForRowAtIndexPath:eachIndexPath];
if(cell.isSelected)
{
// this is our selected cell
}
}
I have a UICollectionView displaying many items in a single section, wrapping over several rows, using UICollectionViewFlowLayout.
Is there a simple way to determine the actual row and column as displayed of a given UICollectionViewCell?
In the example image below, I'm looking for the row & column:
This is just an example - in the actual app I don't know how many columns there are. I realise I could calculate by knowing the cell width, collection view insets and spacing, but I was hoping there was another method that would give me the answer.
you can retrieve the indexPath calling:
NSIndexPath *path = [myCollectionView indexPathForCell: cell];
if each row contains the same number of items
NSInteger itemsPerRow = 2;
NSInteger row = path.item / itemsPerRow;
NSInteger column = path.item % itemsPerRow;
I have a tableView who's datasource is an array with 400 static items in it. In my app you can select a row and it will place a checkmark on that row. I'm keeping track of the checked item's indexPaths in another array so that the table can be reloaded later and those items will still be checked.
Since my table has a lot of values in it I've added a searchDisplayController. Start typing what your looking for and it'll filter the list down to those items. You can select rows in the searchResultsTableView and it will check them, just like in the main table with the 400 static rows. However, there's a problem:
Let's say you enter a search and narrow the list of 400 items down to the one you're looking for and you select (checkmark) it. In the full list of 400 items, the one you just searched for might be number 112 in the list, however when you did your search and filtered down to only that one item, instead of adding the indexPath of item 112 to my array that keeps track, it entered the indexPath of item 0 because it was the only item showing in the filtered list.
So when you cancel out of search and go back to the main list instead of their being a checkmark on item 112, there's a checkmark on item 0.
So I'm looking for a way to keep my filtered array in sync with my main tableView datasource.
The relevant bit of my didSelectRowAtIndexPath method:
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [filteredattributesArray objectAtIndex:[indexPath row]];
if(cell.accessoryType == UITableViewCellAccessoryNone){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedItemRows addObject:indexPath]; //Add the index path of checked cell into array to keep track
[tableView reloadData];
} else {
if(cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedItemRows removeObject:indexPath]; //Remove that index path of unchecked cell from index array
[tableView reloadData];
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES]; }
}
Don't use indexPath. Set the tag value on each item in your data array and then keep a list of tags for your "checkedArray". That way you can evaluate the checked items independent of the cells or tableView structure.
Which number is the first object in a NSArray? I originally thought it was 0 but I may be wrong.
Also which number is the first cell in a UITableView? I thought the first indexpath.row was 1 but I also may be wrong?
Are there any links anyone can point me to, where Apple explains this?
Thanks!
-(BOOL) textFieldShouldReturn:(UITextField *)thetextField {
CustomCell *cell = (CustomCell*)[thetextField superview];
NSIndexPath *indexPath = [thetableView indexPathForCell:cell];
NSLog(#"%i", indexPath.row);
Index 0 represents the first element in NSArray.
As for UITableView, you have both a row and section number in an index path. So the first element of the first section is when indexPath.row = 0 and indexPath.section = 0.
In any array, the first object is at index 0. In case of a tableView, it is basically populated with an array, and the first object, which is indexPath.row == 0 denotes first row.
So the answer to both = 0 !
The first object should be 0 as objc is kind of C.
As for the first object of UITableView,it depends on your datasource. If your application has only a section, the datasource usually is a NSArray. Then the first cell corresponding to the datasource has the 0 index (we usually get the index through
-[NSIndexPath row].
If you have several sections, the the datasource usually is a NSArray of NSArray. Then the first cell corresponding to the datasource is the first object in the first NSArray in the datasource.
You should read the UITableView Programming Guide.
You are assuming that you get a cell with this:
CustomCell *cell = (CustomCell*)[thetextField superview];
Is that assumption correct?