I'm trying to figure out the best way to do this - I have a UITableView of items which the user has previously selected and which is stored. When you click an item it takes you to a detail page. What I need is to be able to click a button below the table view which reloads the table and changes the accessory so its a tick instead of a disclosure, then the user can un-tick the items and remove them from the list, before clicking another button which reloads the updated table and restores the disclosure accessory.
Question is, what is the best way to "remember" which way to handle the table reload after the click so it knows which way to display it? Would you use the NSUserDefaults to store a flag on the button click or is there a more elegant way to do it? I guess I could use the status of the button, whether it's in one state or another, but I'm guessing there is something in-built I'm missing.
Hope that makes sense - thanks.
Usually I have some 'model object' and depending on array of those objects I construct table. My cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
InterestsCell *cell = (InterestsCell*)[tableView dequeueReusableCellWithIdentifier:kInterestsCellID];
if (cell == nil) {
...
}
Interest *i = (Interest*)[self.interestsArray objectAtIndex:indexPath.row];
cell.myTfSubview.enabled = i.isChecked;
return cell;
}
If you don't have your model then you can create array of BOOL values and store flags there.
Related
I'm trying to create a project similar to Instagram. When you select user, it brings you to user VC. When you select comment, it brings you to comment VC.
I have a custom UITableViewCell that has many items in it just like Instagram as mentioned above. What I'm trying to achieve is when user selects any item within a UITableViewCell regardless it is a label, or image, it will do segue to another VC for detailed information.
I'm referring to this post to achieve it, but my concern is how am I going to recognise which has been selected? Because I have an unknown repetitive count of rows.
I don't really need codes as they can most likely be found with thorough searching, though it would be more helpful. But I just need structure because I'm totally lost on how I should do this.
I'm doing this in Swift programming.
Thanks for helping.
You can insert UIButtons into UITableViewCells. Those UIButtons won't conflict with the UITableViewCells tap gesture. I would subclass UIButton so you could pass that row and item data to that button when created
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%ld",(long)indexPath.row);
if (indexPath.row==0)
{
[self performSegueWithIdentifier:#"abc" sender:self];
}
if (indexPath.row==1)
{
}
}
I'm using Xcode 5.1 with iOS7. In my app I have a static uitableview made up of several sections, each has 2-3 rows. I used a static table because this my settings view controller and the contents will not change.
In one such section I have 2 rows. Each row has a label on the right side, similar to the iPhone's General --> Auto-Lock rows.
Like the auto-lock row, when the user selects the row it segues to another tableview controller made up of several rows. Whenever a user selects a row here, I show a checkmark. When the user backs out of this vc I show the name of their selection in the label on the right-side of the row, again just like the auto-lock example.
I use an unwind segue to pass the selected value back (from detail to master, you could say) so I know what the user selected and that's how I set the value in the label that's sitting on the row.
What I'd like to do is be able to already have the detail tableview row checked with the last value the user selected (again, like the auto-lock example). I know the name of what they selected because I send it over in the prepareForSegue. I don't know how to tell the tableview which row to put the checkmark because the detail table doesn't have any connection to the master table, sending 'indexPath.row' wouldn't work. Because I have a small amount of rows, I could just check each row and see if its text is equal to the text that I sent over. I'm not sure if that's the best way to do it, though, because I'd like to allow the user to enter their own text in these labels in a future release. I'd appreciate any ideas. Thanks!
EDIT: I should better clarify my issue and what help I'm looking for. I have 2 rows in a Settings table that both segue to a detail table. This detail table contains several rows of static data. At the time the user selects a row from this detail table, I don't know which row from the Settings table is making the request; I just hand back the label on the row thru an unwind segue. Because of this, I need to be able to look up the value of the data (sent from the Settings table) on the 'viewDidLoad' of the detail table. I'm sorry for not making this clearer from the beginning.
I would simply record the row (or also section, if you want) under a NSUserDefaults key. In viewDidAppear you can set the checkmark.
This would work in any event. If you allow editing and, say, deletion of a row, you can adjust this setting on the fly. Equally, changing the text of the label would be irrelevant.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (indexPath.section == kTrackedSection) {
NSInteger selectedRow = [[NSUserDefaults standardUserDefaults] integerForKey:kSetting];
if (selectedRow == indexPath.row) { return; }
UITableViewCell *otherCell = [tableView cellForRowAtIndexPath:indexPath];
otherCell.accessoryType = UITableViewCellAccessoryNone;
[[NSUserDefaults standardUserDefaults] setInteger:indexPath.row ForKey:kSetting];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
I have created a UITableView populated by data coming from mysql (using NSJSONSERIALIZATION). Now the problem is one thing. What I retrieved was the product name. I want to have an accessory view (arrow like on the right side of cell). once clicked, new view and load details of that product there.
I know it can be done, but don't know how or where to look for a good tutorial. Any recommendations?
am really new to iOS development.
thanks a lot
Use this tableview delegate.
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
...and in cellForRowAtIndexPath: add this.
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
I use a UITableView to display a list of items, and when a row is selected I show that item in a detail view next to it (using a split view controller).
As the selection in the table view changes I want my detail view to change as well, which is of course standard behavior and it works flawlessly... except when no row is selected anymore in the table view.
Users can for example delete a row from the table view, and during and also after that deletion there will be no selected row in the table view. Of course after a row is deleted I let my detail view controller know, but during the editing process in the table view, when no row is selected, my detail view controller does not know this.
I tried to use the UITableViewSelectionDidChangeNotification but that only gets sent when the user selects a different row, not when a deselection occurs.
How can I be notified of the fact that the table view switches from having a row selected to having no selection at all?
EDIT:
As a temporary solution, I tried renaming the Edit-button to "Reorder" and only allowing moving rows from A to B but without allowing the deletion controls, but this can not be done, there is no move control without enabling editing on a row. The thing is, I do get a pointer to a row that is up for moving, so I can keep that selected. I do not get this pointer for a row up for deletion, so no go for me. I always want to know what row a user works on, and keep that row selected at all times.
I may have to resort to ditching the standard editing behavior and adding my own buttons and methods for it. Or see what gestures can do for me to capture touches on any row...
There is a property indexPathForSelectedRow. I'm not sure what it would return when there is no selected row, but it may return nil or something like that, which you could use as a trigger to know if there are any rows selected. Something like
if (myTable.indexPathForSelectedRow == nil)
{
//do something here to account for no rows being selected
}
else
{
//do stuff here to set up your detail view
}
Let me know if this works for you.
How are you implementing the deletion of rows? Are you implementing the following data source method?
tableView:commitEditingStyle:forRowAtIndexPath:
You can have a property like
NSIndexPath *selectedIndexPath
and set it to nil when a row is being deleted.
Example:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//select another row in your table
[tableView selectRowAtIndexPath:*indexPathOfTheRowToBeSelected* animated:YES scrollPosition:UITableViewScrollPositionNone];
//then delete row from data source
}
The method above will be called when the user touches the delete button on the table he/she has selected to delete.
Okay I misread what you were asking at first, I'm pretty sure what you want is either – tableView:willDeselectRowAtIndexPath: or – tableView:didDeselectRowAtIndexPath:. These methods will be triggered automatically when the user deselects a row (either right before the row is deselected or right after, respectively). Implement one of these in your table view delegates code, and you'll know whenever a row gets deselected, and you can put your code to select the next row or whatever you want in one of these methods.
I struggled with this as well. Turns out that when the "Edit" button is hit, all rows are deselected at that point. You can override setEditing:animated: to detect when it is hit, but you must call [super setEditing:editing animated:animated] before you return.
See this link for where I found this:
How can i identify self.editButtonItem button's clicked event?
Ok, I have a grouped TableView that has the following overridden method:
-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
return nil;
}
... Obvious enough, to disable selection.
BUT!
If the user presses and holds on a cell, it gets highlighted (selected)!! I need to disable this, too.
On a side note, I am using the tableView to display static Data, almost like the About tableView in Settings > General. It just loads the info from an array of strings that I created manually.
If there is a better way to represent the data, please do tell!
Thanks!
Set the selectionStyle property of the cell to UITableViewCellSelectionStyleNone.
See the documentation for tableView:willSelectRowAtIndexPath: to see why this is needed even though you're indicating that you don't want the cell selected.
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
...did the trick.