How to get uitableViewCell for specific row in specific section swift - uitableview

How would you get the UITableViewCell for a specific row in a specific section of a tableView in swift?

func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell?, but it only returns a cell if the cell is visible.

Related

Swift: How to access a label in a custom cell within a certain tableView section

I have a custom UITableViewCell with a UILabel called CellLabel. I want to be able to access this label at a certain tableView section e.g. section 3 and provide it a string to display without the use of an array and arrayName[indexPath.section].
You can catch it in cellForRowAtIndexPath override of a TableView
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("YourCustomCellRegisterName") as YourCustomCell
if(indexPath.section == 3){
cell.CellLabel.text = "foo"
}
return cell
}

Swift 2 colored background of UITableViewCell at certain indexPath.rows

I have an iOS Xcode 7.3 project written in Swift 2. I have a UITableView that contains an array of information of type [String] called details. When the user clicks on one of the UITableViewCells, it segues them to another ViewController where they can enter notes for that selected detail. Afterwards they can segue back to the UITableView. My goal is if a detail in the UITableView has a note attached that was typed, that cell's backgroundColor would be yellow.
Below is:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Transfer details of selected cell to notes view
var selectedCell = details[indexPath.row]
detailsTransfer = selectedCell // Transfer detail information
detailLocation = indexPath.row // Transfer detail location in array
}
This transfers the details and location in the array for adding notes to the detail. I then save the detailLocation to a new array of type Int. I'm thinking to get the color change in the cell background, I'd need to loop through the [Int] and only those cells in the UITableView would have a backgroundColor of yellowColor()? I don't know if this is correct and/or how/where this code would go. Maybe the loop in: tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! ?? Can someone please help? Thank you.
UPDATE
My cellForRowAtIndexPath is:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cellIdentifier = "LogTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! LogTableViewCell
cell.logLabel?.text = details[indexPath.row]
return cell
}
Still trying to compare indexPath.row for color change.
In the main table view, tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! method, you can do something like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
// Transfer details of selected cell to notes view
var selectedCell = details[indexPath.row]
detailsTransfer = selectedCell // Transfer detail information
detailLocation = indexPath.row // Transfer detail location in array
if selectedCell.hasNote {
cell.backgroundColor = UIColor.yellowColor()
}
}
You don't need to loop because the table view data source already checks each row for you.

Add custom UIView with different height into UITableViewcell one after another top to bottom

Hi I am newbie of swift ios, how can I add custom UIView(s) with different height (depends on condition) into UITableViewCell one by one, and one below another. Just like the reminder app in iphone but each cell with different/same customViews - depends on condition. What's the best approach or practice for this. Thanks gurus.
To use custom views into UITableViewCell you have to add a xib file and add to a class extended of UITableViewCell.
To specify your custom view and UITablaView you must do it here:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell.
Example:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("yourCellIdentifier") as! CustomView
...
return cell
}
To set auto height and more info you can follow this tutorial:
dynamic table view cell height ios 8 swift

What is the use of tableview's delegate method cellforrowatindexpath in swift?

i am fresher and i am doing my app in swift language and i want to know that what is the difference between
func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?
and
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell/
delegate methods in UITableView?
As a one line answer to the question,
The first method
func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?
is not tableView delegate method. It an instance method in UITableView Class. Which is used to get a cell from tableView instance by passing indexPath as parameter. So you will be using this method on a tableView instance.
The second method
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
is the datasource method of UITableView that is used to populate tableview cells. This method will be implemented in the datasource/delegate class.
func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell? is a UITableView method, it is used to get a cell at a given time when the tableview is defined. For instance, it can be used to check is a cell is visible. If it is not visible this method will return nil.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell is a UITableViewDataSource method, it is used to define all the cells of the tableview, so that it can know what to display.
The first method
func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?
It is an instance method in UITableView Class. And it is used to get a cell from tableView instance by passing indexPath as parameter .
and the second one
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
is the datasource method of UITableView and it is one of the require method in UITableView.
Thank you.

UITableViewCellAccessoryType.Checkmark shows checkmark only when another row is tapped

I have the following code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = users[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
The purpose was to show a checkmark when a row is tapped. Have two rows and I am observing the following behavior.
Tap row 0, can't see the checkmark
Tap row 1, can see the checkmark for the row 0
Tap row 0, can now see the checkmark for row 1
The selection attribute settings are (Xcode default)
Selection: Single Selection
Editing: No selection during editing
Is there anything I should do to show the checkmark when the row is tapped in addition to the above code. Looked into related questions but couldn't nail it.
This is a very common problem, I think almost everybody who does iOS programming has faced the same.
You need to change
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
to
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
The issue is with the XCode suggestions as one types the UITableView delegate methods. Since deselect comes first alphabetically, it is a very common mistake. I used to face this issue with Objective C, it's funny to see this problem arising with Swift as well.

Resources