Differentiate indexPath from static table's sections - swift

I can get the row number by indexPath.row in didSelectRowAtIndexPath, and different section are getting the same index like 0,1,2,3; 0,1; 0,1,2 ... How can I find out which section is being selected?

You can use NSIndexPath section property
indexPath.section

Related

UICollectionview item number

I have UIcollectionview with sections. In cellForItemAtIndexPath i can see indexPath.section and indexPath.item. How can i know number of the item from the very beginning of the first section?
There is no built-in method. But you can add the number of items of all previous
sections to the item number within the current section:
NSInteger num = indexPath.item;
for (NSInteger section = 0; section < indexPath.section; section++) {
num += [collectionView numberOfItemsInSection:section];
}
But perhaps you should also check if you really need this. For example, if the reason is
that your data source is a flat array then you might consider to rearrange the data source,
reflecting the two-level sections/items structure.
You need the cell at indexPath (0,0), so try:
NSIndexPath * indexPath = [NSIndexPath indexPathForItem: 0 inSection: 0];
and then use cellForItemAtIndexPath as before
You might be looking for indexPath.row.

How to disable UITableViewCell selection one by one?

I am creating a table view based iPhone application, Where i need to disable one by one entire table view cell.
The requirement is like -
Case-1 :-
Initially only first table cell row should be user interaction enable, rest should be disable. We can easily do this to make indexPath.row 0 enable in CellForRowAtIndexPath method.
Case-2 :-
If User will tap on First cell and when again he will come back then First cell will be disable and second cell should enable(rest table cell will be disable at this time after second cell).
Case-3 :-
Again if user will tap on second table cell and come back to this table then apart from Third cell, others should be disable and so on...
It means i need to disable one by one all the table cell in serial order.It is possible by setting index but it won't be the proper way to do this. So, can you please suggest me what condition i need to give for this ? Please suggest me for further proceeding.
Thanks.
Just maintain an instance variable which holds the cell number that should be enabled.e.g. enabledCell.
Initialize enabledCell to 0. In didSelectRow increment enabledCell and reload the tableView. In cell for row at indexPath only enable the cell if the indexPath is same as the enabledCell value.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// write code to dequeue or create new UITableViewCell here
// then check if index is same as the cell that should be enabled
if(index.Path.row == enabledCell)
cell.userInteractionEnabled = NO;
else
cell.userInteractionEnabled = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//Assuming you have number of rows stored in variable numberOfRows
enabledCell++;
if(enabledCell == numberOfRows)
enabledCell = 0;
[tableView reloadData];
}
There are many ways to do this (from now on, I'm a assuming a linear table view with n rows and only one section).
For example, you can add a BOOL enabled property to your cell model object. In your controller's didSelectRowAtIndexPath: check that property and do nothing if it's set to NO. If it's set to YES then navigate to the appropriate page and exchange the enabled property with the one at index position i + 1 (or position 0 if it's the last entry and you want to cycle).

XCode edit UITableView textFields for different rows

I have a program that displays a table with just 1 Cell row with the customer's information to be keyed in using 2 textFields. (Shown below)
The user is able to click on the Addition sign to add a new row to their interests.
When the user selects the Main Category, the Sub Category should be auto filled with information.
My problem is, when i have say 2 rows of cells, I'm unable to auto populate the second subCategory with the selection from the mainCategory because I have no idea how to identify them.
I've implemented both the UITableView/UIPickerView delegates in this view.
Use the convenient tag attribute of the text fields. You can set these in Interface Builder inside Xcode to distinguish between your two fields.
Now you can identify the particular row and its index path like this, say, in textField:didBeginEditing:
#define mainTag 2
#define subTag 3
UITableViewCell *cell = (UITableViewCell*)textField.superview.superview;
NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
UITextField *mainCategory = (UITextField*)[cell viewWithTag:mainTag];
UITextField *subCategory = (UITextField*)[cell viewWithTag:subTag];
The trick is that the text field is a subview of a view called contentView which is part of every UITableViewCell, which again is a subview of the cell itself.

How can I add/remove a cell from grouped section uitableview?

How can I programmatically add or remove a cell from a grouped section?
I created a grouped tableview using static cells in storyboard. Inside the storyboard I set the number of rows using the Attribute Inspector panel. For example, for section 1, I define 3 rows. Then using an NSMutableArray of 3 items, I can properly load values into each section correctly at startup.
I ultimately want the ability to add/remove a cell at runtime. That part hasn't been coded yet but to simulate adding a new cell scenario, I added a new item to the array in the code but did not increase the row count for the section in the Attribute Inspector panel. I had hoped that I would not need to make any other changes to accomodate the new item since inside the numberOfRowsInSection method, I'm returning the count of the array for the specific section.
This is the error message that I get when I re-ran the code:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
Apparently I need to somehow specify an additional row count. Can anyone shed some light on how I can do this at runtime? Thanks.
You should not do it through attribute inspector. it abstains you to do dynamic insertion and deletion of cells.
try
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [yourArray count];
}
else do this.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section==0){
return 3;
}else if(section==1){
return 4;
}
return 0;
}
and so on for different section if u have many section..
you can reload the whole table when you array from which data is read is updated..
[tableView reloadData];
but a better way to do it is to insert row or multiple rows like this in a block.
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:yourIndexPath withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
you can also add animation to add cell from right or lect or top or bottom.
I hope this helps you!!
Cheers!!
I guess that you hardcoded the number of rows for your section. Because looking at the error it seems that your section is expecting 4 object but your data source contains only 3.
Inside your :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
you should return something like [myArray count], so adding or delting rows shouldn't be a problem if you add or remove the object from your array.
You have Predefined methods in UITableview for Inserting and deleting row, For insertion we can user insertRowAtIndexPath method and for deletion use deleteRowAtIndexPath methods.

Referencing UISwitches within UITableCells

I have a table view which I'm using for some settings in my app. The tables cells are all default (no customisation at all), and simply contain some text for their label and a UISwitch for the accessory view.
My problem is that I need a reference to the switch so that I know when it has been switched on and off.
Currently I am setting the 'tag' property of the switch to be that of the cell's index within the table (grabbed from [indexPath row] in tableView:cellForRowAtIndexpath:).
This is fine when you only have one Section in your table, but I am now adding a new section. The problem is that they are both 0 based indexed so the switches in each section will end up reusing the tags - which isn't good.
Any suggestions on a better way to achieve this?
Thanks.
If you know roughly how many sections and rows you will have, like oh, say, not more than 1 million rows per section, just hash the section and row like this:
const int oneMillion = 1000000;
int tag = (section * oneMillion) + row;
slider.tag = tag;
Then, to figure out the section and row, reverse the logic:
int tag = slider.tag;
int row = tag % oneMillion;
int section = tag / oneMillion;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: row inSection: section];
Now get the slider that is in the cell in that section,row of the table
UITableViewCell *sliderCell = [tableView cellForRowAtIndexPath: indexPath];
UISlider *slider = [[sliderCell.contentView subviews] objectAtIndex: 0];
This assumes the slider is always the only view in the contents of the cell.
This method is a bit longer than some of the other suggestions above, but it keeps you from having to cache references off to the side.
For each cell, set a delegate link back to the table view controller, and also some kind of row reference ID - then wire the switch to a cell IBAction method, that calls back to the delegate with the reference ID for that cell.
What you can do is either have an Array of arrays or a dictionary, key it by the section number (or in case of the array they will be in order of the section numbers), now to retreive a switch all you do assuming you know the section and the row number
UISwitch *switch=[[switchArray objectAtIndex:section] objectAtIndex:row];
or if you have a dictionary
UISwitch *switch=[[switchDictionary objectForKey:section] objectAtIndex:row];