Manually call didSelectRowatIndexPath - ios

I am trying to call didSelectRowAtIndexPath programmatically but am having trouble.
[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];
Gives me the following error:
Use of undeclared identifier 'indexPath'; did you mean 'NSIndexPath'?
Can anybody help me out? Thanks!
EDIT: From the responses below it sounds like im going about this the wrong way. How can I get the text of the selected items when a button is pressed (can be multiple selections)? I need to do this in a function dedicated to the button press.

You need to pass a valid argument, if you haven't declared indexPath in the calling scope then you'll get that error. Try:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:ROW_YOU_WANT_TO_SELECT inSection:SECTION_YOU_WANT_TO_SELECT]
[self tableView:playListTbl didSelectRowAtIndexPath:indexPath];
Where ROW_YOU_WANT... are to be replaced with the row and section you wish to select.
However, you really shouldn't ever call this directly. Extract the work being done inside tableView:didSelectRowAtIndexPath: into separate methods and call those directly.
To address the updated question, you need to use the indexPathsForSelectedRows method on UITableView. Imagine you were populating the table cell text from an array of arrays of strings, something like this:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeue...];
NSArray *rowsForSection = self.sectionsArray[indexPath.section];
NSString *textForRow = rowsForSection[indexPath.row];
cell.textLabel.text = textForRow;
return cell;
}
Then, to get all the selected text, you'd want to do something like:
NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
NSMutableArray *selectedTexts = [NSMutableArray array];
for (NSIndexPath *indexPath in selectedIndexPaths) {
NSArray *section = self.sectionsArray[indexPath.section];
NSString *text = section[indexPath.row];
[selectedTexts addObject:text];
}
selectedTexts would at that point contain all selected information. Hopefully that example makes sense.

Swift Solution
Manually call didSelectRowAtIndexPath
let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .top)
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)

Just to update #Sourabh's answer, note that you can also provide a scrollPosition when calling selectRow:
let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true)
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
Becomes:
let indexPath = IndexPath(row: 7, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .top) // <--
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)
The possible constants are:
.none
The table view scrolls the row of interest to be fully visible with a
minimum of movement. If the row is already fully visible, no scrolling
occurs. For example, if the row is above the visible area, the
behavior is identical to that specified by top. This is the default.
.top
The table view scrolls the row of interest to the top of the visible
table view.
.middle
The table view scrolls the row of interest to the middle of the
visible table view.
.bottom
The table view scrolls the row of interest to the bottom of the
visible table view.

You need to define an indexPath variable if you don't already have one to stick in there.
Something like:
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

Starting from Mistalis answer, I made this little ExtendedCell class because I use it in several table views.
Usage in table view:
// Here MyTableView is UITableView and UITableViewDelegate
// but you can separate them.
class MyTableView: UITableView, UITableViewDelegate {
override func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell? {
return MyCell(identifier: identifier, table: self)
}
// ...
}
Implementation of your cell:
class MyCell : ExtendedCell {
convenience init(identifier: String?, table: MyTableView)
{
// in this example, MyTableView is both table and delegate
self.init(identifier: identifier, table: table, delegate: table)
// ...
}
// At any moment you may call selectTableRow() to select
// the row associated to this cell.
func viewItemDetail() {
selectTableRow()
}
}
ExtendedCell class:
import UIKit
/**
* UITableViewCell with extended functionality like
* the ability to trigger a selected row on the table
*/
class ExtendedCell : UITableViewCell {
// We store the table and delegate to trigger the selected cell
// See: https://stackoverflow.com/q/18245441/manually-call-did-select-row-at-index-path
weak var table : UITableView!
weak var delegate : UITableViewDelegate!
convenience init(identifier: String?, table: UITableView, delegate: UITableViewDelegate)
{
self.init(style: .default, reuseIdentifier: identifier)
self.table = table
self.delegate = delegate
}
func selectTableRow()
{
if let indexPath = table.indexPath(for: self) {
table.selectRow(at: indexPath, animated: true, scrollPosition: .none)
delegate.tableView!(table, didSelectRowAt: indexPath)
}
}
}

Related

Why is tableView(_: cellForRowAt indexPath:) called so many times leading to a weird UIButton bug?

I'm just making a simple project for fun but I am having a very strange bug in my program.
This function returns 29 which is correct however it is called three times when my table view is instantiated.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
print("ingredient count: \(Sushi.ingredients.count)")
return Sushi.ingredients.count
}
And I think that (_: numberOfRowsInSection:) being called 3 times is what leads this code to execute almost three times the number of elements in the ingredients array.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "IngredientCell", for: indexPath) as? IngredientCell
else
{
return UITableViewCell(style: .default, reuseIdentifier: nil)
}
cell.ingredientLabel.text = Sushi.ingredients[indexPath.row]
print("ingredient label text: \(cell.ingredientLabel.text)\nindex path: \(indexPath)\ncell index: \(cell.index)\nbutton tag: \(cell.selectButton.tag)\nindex path row: \(indexPath.row)\ncell object: \(cell)\n")
return cell
}
The table view is populated appropriately as far as it lists all the ingredients in the array correctly. However, each row of the tableView is a prototype cell that has a UILabel, a UIButton, and a UIImage. When the button is pressed the text and color change for that button but also for every thirteenth button in the tableview cells.
This class is for my individual cells
class IngredientCell: UITableViewCell
{
public var userSelected = false
public var index: Int = 0
#IBOutlet weak var picture: UIImageView!
#IBOutlet weak var selectButton: UIButton!
#IBOutlet weak var ingredientLabel: UILabel!
//This method is called when a button is pressed
#IBAction func selectIngredient(_ sender: UIButton)
{
if userSelected == false
{
userSelected = true
selectButton.setTitleColor(.red, for: .normal)
selectButton.setTitle("Remove", for: .normal)
}
else
{
userSelected = false
selectButton.setTitle("Select", for: .normal)
selectButton.setTitleColor(.blue, for: .normal)
}
}
}
I decided to debug using print statements to see if I could figure out what exactly is going on and I found that numberOfRowsInSection is called 3 times and cellForRowAtIndexPath is called a varying amount of times. I keeps iterating over the table view giving the cell objects the same memory addresses which I suppose is what causes multiple buttons to change when only one is pressed. My console output proved that different buttons in the storyboard had the same addresses in memory.
Sorry, I can only write in Objective-C. But anyway, I am just going to show you the idea.
One of the ways to update the UI contents of just a single item of a table is to reload that specific cell.
For example: Your cell contains just a button. When you press a specific button you want to change the title of that button only. Then all you have to do is to create an dictionary which references all the titles of all buttons in your table, and then set the title to that button upon reload of that specific cell.
You can do it this way:
#interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
#property NSMutableDictionary *buttonTitlesDict;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
buttonTitles = [[NSMutableDictionary alloc] init];
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"SimpleTableItem";
UITableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(tableCell == nil)
{
tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
tableCell.button.title = [_buttonTitles objectForKey:#(indexPath.row)];
tableCell.button.tag = indexPath.row;
}
//reloads only a specific cell
- (IBAction)updateButton:(id)sender
{
NSInteger row = [sender tag];
[_buttonTitlesDict setObject:#"changedTitle" forKey:#(row)];
[_tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
As you can see, I just created a dictionary that holds the button title for each button with the index of the cell as the reference key. Every time I want to update the title of the button, I will get that title from the button every time the cell is loaded upon cellForRowAtIndexPath. take note that when you scroll the table, cellForRowAtIndexPath will be called when new cells appear in front of you. So as you scroll, the correct titles will be updated to the buttons.
Hope this helps.

Remove All Cell Accessories in UITableView in Swift 2

I have a method in Objective-C that I've used to uncheck all cells in a UITableView:
- (void)resetCheckedCells {
for (NSUInteger section = 0, sectionCount = self.tableView.numberOfSections; section < sectionCount; ++section) {
for (NSUInteger row = 0, rowCount = [self.tableView numberOfRowsInSection:section]; row < rowCount; ++row) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = nil;
}
}
}
In Swift, I think I need to use enumeration to accomplish this. I'm stumped as to how to get the values I need. Here's a "physics for poets" sketch of what I'm trying to do:
func resetCheckedCells() {
// TODO: figure this out?
for (section, tableView) in tableView.enumerate() {
for (row, tableView) in tableView {
let cell = UITableView
cell.accessoryType = .None
}
}
}
This doesn't work, but it's illustrative of what I'm trying to accomplish. What am I missing?
UPDATE
There was a very simple, but non-apparent (to me), way to do this involving cellForRowAtIndexPath and a global array...
var myStuffToSave = [NSManagedObject]()
... that's instantiated with the UITableViewController loads. I'm posting this update in hopes that someone else might find it helpful.
My UITableViewController is initially populated with NSManagedObjects. My didSelectRowAtIndexPath does two things:
1) adds/removes NSManagedObjects from a global myStuffToSave array
2) toggles cell.accessoryType for the cell between .Checkmark and .None
That when cellForRowAtIndexPath is called, I compare items from myStuffToSave with what's in the tableView.
Here's a snippet of my cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
// I set the cells' accessory types to .None when they're drawn
// ** SO RELOADING THE tableView NUKES THE CHECKMARKS WITH THE FOLLOWING LINE... **
cell.accessoryType = .None
// boilerplate cell configuration
// Set checkmarks
// ** ...IF THE ARRAY IS EMPTY
if self.myStuffToSave.count > 0 {
// enumerate myStuffToSave...
for (indexOfMyStuffToSave, thingToSave) in stuffToSave.enumerate() {
// if the object in the array of stuff to save matches the object in the index of the tableview
if stuffInMyTableView[indexPath.row].hashValue == stuffToSave[indexOfMyStuffToSave].hashValue {
// then set its accessoryView to checkmark
cell.accessoryType = .Checkmark
}
}
}
return cell
}
So removing everything from myStuffToSave and reloading the tableView will reset all the checked cells. This is what my resetCheckedCells method looks like at the end:
func resetCheckedCells() {
// remove everything from myStuffToSave
self.myStuffToSave.removeAll()
// and reload tableView where the accessoryType is set to .None by default
self.tableView.reloadData()
}
Thanks to #TannerNelson for pointing me towards a solution.
This seems like a strange way to use UITableView.
You should look at the UITableViewDataSource protocol and implement your code using that.
The main function you will need to implement is tableView:cellForRowAtIndexPath. In this function, you dequeue and return a cell.
Then when you need to update cells to be checked or unchecked, you can just call reloadAtIndexPaths: and pass the visible index paths.
This gist has a nice UITableView extension for reloading only visible cells using self.tableView.reloadVisibleCells()
https://gist.github.com/tannernelson/6d140c5ce2a701e4b710

how to stop another rows to be select after select a row in uitableview

In tableview selcting a row in didselectrow i used this
iPhone :UITableView CellAccessory Checkmark
by selecting one row i'm callling a webservice
PROBLEM : i want only one row selection not another by next time what to do
stop another rows to being selected
Add tableView.allowsSelection = false to didSelectRowAtIndexPath: and then re-set it to true at the appropriate time (I'm guessing after your web service stuff completes).
ADDED:
To make it so that the selected row cannot be selected again, I would add
Swift:
var selectedRows = [Int]()
as an instance variable (i.e. at the class level, not within a method).
Then I would change didSelectRowAtIndexPath: to something like this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if !(selectedRows as NSArray).containsObject(indexPath.row) {
// Request data from web service because this row has not been selected before
selectedRows.append(indexPath.row) // Add indexPath.row to the selectedRows so that next time it is selected your don't request data from web service again
let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
}
}
Objective-C:
#property (strong, nonatomic) NSMutableArray *selectedRows;
In the view controller, initialize selectedRows:
- (NSMutableArray *)selectedRows
{
if (!_selectedRows) {
_selectedRows = [[NSMutableArray alloc] init];
}
return _selectedRows;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (![self.selectedRows containsObject:indexPath.row]) {
// Request data from web service because this row has not been selected before
[self.selectedRows addObject:indexPath.row]; // Add indexPath.row to the selectedRows so that next time it is selected your don't request data from web service again
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
}

Get the row of UITextField inside UITableViewCell

I have two UITextFields inside a custom cell of a UITableView.
I need to edit and store values of the textFields.
When I click inside a UITextField I have to know the row it belongs to in order to save the value to the correct record of a local array.
How can I get the row index of the textField?
I tried :
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
currentRow = [self.tableView indexPathForSelectedRow].row;
}
But the currentRow does not change when I click inside the UITextFieldRow.It changes only when I click (select) the entire row...
The text field did not send touch event to the table view so indexPathForSelectedRow is not working. You can use:
CGPoint textFieldOrigin = [self.tableView convertPoint:textField.bounds.origin fromView:textField];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:textFieldOrigin];
In iOS 8 I found that the simulator and device had different number of superviews, so this is a little more generic and should work across all versions of iOS:
UIView *superview = textField.superview;
while (![superview isMemberOfClass:[UITableViewCell class]]) { // If you have a custom class change it here
superview = superview.superview;
}
UITableViewCell *cell =(UITableViewCell *) superview;
NSIndexPath *indexPath = [self.table indexPathForCell:cell];
Try this
//For ios 7
UITableViewCell *cell =(UITableViewCell *) textField.superview.superview.superview;
NSIndexPath *indexPath = [tblView indexPathForCell:cell];
//For ios 6
UITableViewCell *cell =(UITableViewCell *) textField.superview.superview;
NSIndexPath *indexPath = [tblView indexPathForCell:cell];
1>You can achieve it by programmatically creating textfields in your CellForRowAtIndexPath and setting text field's tag as indexpath.row.
then textFieldDidBeginEditing you can just fetch textField.tag and achieve what you want.
2>another way is to have 2 custom cells in one table view. in that way you cam place text feild individually and set their tags from your utility panel.
What I do is create a custom cell, and put whatever custom UI elements I need inside and create a property indexPath which gets set when the cell is dequeued. Then I pass the indexPath along to the custom elements in didSet.
class EditableTableViewCell: UITableViewCell {
#IBOutlet weak var textField: TableViewTextField!
var indexPath: IndexPath? {
didSet {
//pass it along to the custom textField
textField.indexPath = indexPath
}
}
}
class TableViewTextField: UITextField {
var indexPath: IndexPath?
}
In TableView:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "EditableCell") as! EditableTableViewCell
cell.indexPath = indexPath
return cell
}
Then I implement the UITextFieldDelegate protocol, and since the textField has its indexPath you will always know where it came from.
Not sure where the best place to set the delegate is. The easiest is to set it when the cell is dequeued.
override func textFieldDidEndEditing(_ textField: UITextField) {
guard let myTextField = textField as? TableViewTextField else { fatalError() }
guard let indexPath = myTextField.indexPath else { fatalError() }
}

UITableView, replace cell view on selection

Is it possible to replace a cell view for the selected cell? I have registered my controller as a data source and delegate. Requests for cell views, row numbers, selection status, etc, all come in nicely. In the cell selection callbacks, I'm trying to have the table view reload the cell with a new view (not the actual data!). Basically, I want the cell view to expand and show more of the underlying data, if it is selected. The problem is that I have no clue how to make the table view ask my controller for a new view. I can see that the view is requesting the cell height, but nothing more.
Calling reloadData on the view works, but it's inefficient and comes with a set of its own problems (animation possibilities, maintaining selection state, etc).
Here is the approach I would take to do this.
#property (nonatomic, strong) NSIndexPath *selectedIndexPath;
Set a property of type NSIndexPath to your controller to store which index path was selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation: UITableViewRowAnimationNone];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath compare:self.selectedIndexPath] == NSOrderedSame) {
// Create your custom cell here and return it.
}
else {
// Should create a normal cell and return it.
}
}
Exactly. Note too that you probably want to deselect. Here's the full code in Swift. Use selectedIndexPath in cellForRowAtIndexPath as appropriate.
// Selecting TableViewController
import UIKit
class SelectingTableViewController: UITableViewController
{
internal var selectedIndexPath:NSIndexPath? = nil
override func viewDidLoad()
{
super.viewDidLoad()
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension
self.clearsSelectionOnViewWillAppear = false;
}
override func tableView
(tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath)
{
print("did select....")
// in fact, was this very row selected,
// and the user is clicking to deselect it...
// if you don't want "click a selected row to deselect"
// then on't include this clause.
if selectedIndexPath == indexPath
{
print("(user clicked on selected to deselect)")
selectedIndexPath = nil
tableView.reloadRowsAtIndexPaths(
[indexPath],
withRowAnimation:UITableViewRowAnimation.None)
tableView.deselectRowAtIndexPath(indexPath, animated:false)
return
}
// in fact, was some other row selected??
// user is changing to this row? if so, also deselect that row
if selectedIndexPath != nil
{
let pleaseRedrawMe = selectedIndexPath!
// (note that it will be drawn un-selected
// since we're chaging the 'selectedIndexPath' global)
selectedIndexPath = indexPath
tableView.reloadRowsAtIndexPaths(
[pleaseRedrawMe, indexPath],
withRowAnimation:UITableViewRowAnimation.None)
return;
}
// no previous selection.
// simply select that new one the user just touched.
// note that you can not use Apple's willDeselectRowAtIndexPath
// functions ... because they are freaky
selectedIndexPath = indexPath
tableView.reloadRowsAtIndexPaths(
[indexPath],
withRowAnimation:UITableViewRowAnimation.None)
}
}
Have you tried:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Then you can specify only your updated cell for reloading. You can also tell your tableview to remember or forget selection status upon reload with:
tableViewController.clearsSelectionOnViewWillAppear = NO;
Then you will also have to deal with the custom view inside
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath

Resources