EDIT 2: Thought I had it but I don't. I need to create the array when the button is pressed, but I can't figure out how to access the .checkmark for each object. I have ".selected" as a property already, but can't access that either.
I have a UITableView that display a list in three sections with checkmarks. When a row in checked, the bool associated with that NSObject class changes from false to true. I have a button in the UINavigationBar that when pressed shows an alert with "Send" and "Cancel" options. When the user taps "Send" I want to have all the rows with checkmarks added into an array. I figured using that bool would be the best way, but in the func for the action I can't call the bool associated with each NSObject in the array. I included the parts of code I think are needed to help me.
EDIT: I believe this is how I have my class set up, unless I'm misunderstanding. One of the class' vars is "var selected: Bool" then when I create an Item from that class I set it to false. I have a list of Items that displays fine in cellForRowAt but when the button is tapped I can't get access to that same "sortedList". Here's more code. Maybe I'm still missing something else?
var arrayOfItemsSelected = [String]()
var sortedItems = [[Item]]()
var itemList: ItemList! {
didSet {
sortedItems = itemList.sortByBrands()
self.tableView.reloadData()
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = sortedItems[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "UIItemViewCell", for: indexPath)
cell.textLabel?.text = "\(item.name)"
cell.detailTextLabel?.text = "\(item.style)"
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none // to prevent cells from being "highlighted"
return cell
}
func confirmButtonPressedAction() {
// Create the alert controller
let alertController = UIAlertController(title: "List of checked items", message: stringOfSelectedItems, preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.default) {
UIAlertAction in
self.arrayOfItemsSelected.removeAll()
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
Your code either isn't doing anything with arrayOfItemsSelected or you're not showing us the relevant parts. As it is, the code sample will neither obtain nor process the selected items.
You could either implement the didSelectRowAt and didDeselectRowAt function to maintain an internal flag in your Item class, or you could get the selection from indexPathsForSelectedRows.
If you want to completely circumvent the UITableView's selection mechanism, you would need to handle cell clicking/tapping with actions in the UIItemViewCell itself (but that seems overly complicated).
BTW, I am doubtful that you can rely on cell.isSelected from a cell you just dequeued. Also, resorting and reloading the tableview in a didSet closure is likely to get you into trouble causing performance issues, random crashes or infinite loops.
If you have a property checked on the class Item you can try:
let itemsChecked = sortedItems.filter {
$0.checked == true
}
You have to add boolean check with each array object.link below
For Objective C:
#[
#{
#"Name":#"abc",
#"Status":[NSNumber numberWithBool:YES]
}
]
For Swift:
[
[
"Name": "abc",
"Status": true
]
]
You have to add boolean check with each array object, for example myModel.
[
"Name": "abc",
"Status": true
]
When a row in checked,
myModel.Status = myModel.Status ? false : true
myModel's default state = false
then " you can call the bool associated with each NSObject in the array"
Related
I'm currently working on an app that has a table-view-like collection view and some other view controllers. Basically, my question is how can I update the indexPath of each cell when one of the collection view cells is deleted.
I attached my view controller file below, but here is what's going on on the app.
When a user opens the table-view-like collection view (in EventCollectionVC), it reloads the data from a database and presents them on the collection view. I also added the code to the navigation bar button item that the user can change the collection view to the edit mode. While in the edit mode, a small ellipsis.circle (SF symbols) is displayed on the collection view cell. When a user taps the ellipsis.circle icon, it displays a new view controller (ModalVC) and lets the user select either delete or edit the cell. When the user selects delete, it shows an alert to delete the cell and delete the cell information with modal dismiss (which means the ModalVC is closed and the MyCollectionVC is displayed now).
Since I have to make the two view controllers (like getting cell information from EventCollectionVC and present in ModalVC) talk to each other, I need to use the indexPath.row to get the information of the cell. Before deleting the cells, the numbers of indexPath.row in the collection view is like
[0,1,2,3,4,5]
But, for example, after I delete the second (indexPath.row = 1) cell and when I try to delete another item, the indexPath becomes
[0,2,3,4,5]
and I can see the collection view's index is not refreshed.
So my question is how can I update/refresh the cell's indexPath.row value after I delete a cell from the collection view?
This is the code with some explanations.
import UIKit
class EvnetCollectionViewController: UIViewController {
var EventDataSource: EventDataSource! // <- this is a class for the Model, and it stores array or Events
let ListView = ListView() // view file
var collectionViewDataSource: UICollectionViewDiffableDataSource<Section, Event>?
var targetEventIndex = Int() // variable to store the index of the event when event cell is tapped
override func loadView() {
view = ListView
}
override func viewDidLoad() {
super.viewDidLoad()
configureNavItem()
setupCollectionView()
displayEvents()
}
func configureNavItem() {
navigationItem.rightBarButtonItem = editButtonItem
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
if (editing){
ListView.collectionView.isEditing = true
} else {
ListView.collectionView.isEditing = false
}
}
func setupCollectionView() {
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Event> { cell, indexPath, Event in
var content = UIListContentConfiguration.cell()
content.text = Event.title
cell.contentConfiguration = content
let moreAction = UIAction(image: UIImage(systemName: "ellipsis.circle"),
handler: { _ in
let vc = EventActionModalViewController(); // when the user select the cell in edit mode, it displays action modal VC and then come back to this VC with dismiss later
vc.modalPresentationStyle = .overCurrentContext
self.targetEventIndex = indexPath.row // I need targetEvemtIndex when user selects delete event in EventActionModalVC, so just sotre value in here
})
let moreActionButton = UIButton(primaryAction: moreAction)
let moreActionAccessory = UICellAccessory.CustomViewConfiguration(
customView: moreActionButton,
placement: .trailing(displayed: .whenEditing, at: { _ in return 0 })
)
cell.accessories = [
.disclosureIndicator(displayed: .whenNotEditing),
.customView(configuration: moreActionAccessory)
]
}
collectionViewDataSource = UICollectionViewDiffableDataSource<Section, Event>(collectionView: ListView.collectionView) {
collectionView, indexPath, Event in
collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: Event)
}
}
func displayEvents() {
EventDataSource = EventDataSource()
EventDataSource.loadData() // get Events in db and sore in an array Events
populate(with: EventDataSource.Events)
}
func populate(with Events: [Event]) {
var snapshot = NSDiffableDataSourceSnapshot<Section, Event>()
snapshot.appendSections([.List])
snapshot.appendItems(Events)
collectionViewDataSource?.apply(snapshot)
}
func showDeleteAlert() {
let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { _ in
self.EventDataSource.delete(at: targetEventIndex)
self.refreshList()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
self.showAlert(title: "Delete", message: nil, actions: [deleteAction, cancelAction], style: .actionSheet, completion: nil)
}
func refreshList() {
EventDataSource.loadData()
setupCollectionView() // since I write this code, it updates all of the indexPath in the collection view, but after deleting one item, the whole collection view is deleted and new collection view is reappeared.
populate(with: EventDataSource.Events)
}
}
I kinda know why this is happening. I only configure cell (in let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Event>...) once, so it won't update the cell information as well as its index path until I configure it again. But if I call setupCollectionView every after deleting one item, the whole collection view disappears and shows up again. Is it possible to reload the collection view list and updates its information without reloading the entire collection view?
Without writing setupCollectionView() in refreshList, the cell's indexPath is not refreshed and I get an error after I delete one cell and try to delete another one. So, I was wondering if there is a way to avoid recreating the whole collection view but update cells' indexPath when the user delete one of the cell in collection view.
I fixed the code in the refresh list function.
func refreshList() {
self.EventDataSource.loadData()
self.populate(with: self.EventDataSource.Events)
ListView.collectionView.reloadData()
}
I just needed to call reloadData after I populate all the data...
I have simple chat created using UITableView. I want to add the ability to highlight a message after long press. In fact, I want to create the same feature like iMessage:
After a long press, we unhighlight background (more darker), highlight message, scroll to this message and show the actionSheet
For now I managed to add only longPress and actionSheet
Long press recongizer on viewDidLoad:
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(onCellLongPressed(gesture:)))
messagesTableView.addGestureRecognizer(longPressRecognizer)
onCellLongPressed function:
#objc func onCellLongPressed(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
let touchPoint = gesture.location(in: self.messagesTableView)
if let indexPath = messagesTableView.indexPathForRow(at: touchPoint) {
self.messagesTableView.selectRow(at: indexPath, animated: true, scrollPosition: UITableViewScrollPosition.none)
shareWithFriend()
}
}
}
#objc func shareWithFriend() {
alert(style: .actionSheet, actions: [
UIAlertAction(title: "Share with friend", style: .default, handler: { [weak self] (_) in
print("SHARE HERE")
}),
UIAlertAction(title: "Cancel", style: .destructive),
])
}
func alert(_ title: String? = nil, message: String? = nil, style: UIAlertController.Style, actions: [UIAlertAction]) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: style)
actions.forEach(alertController.addAction)
present(alertController, animated: true)
}
As you can see, the background color is above the navigation bar so I guess there is a secondary view controller astutely presented above the collection view when the user selects a cell.
I think this is two different view hierarchies which look like one:
One view controller contains the balloon list
One view controller contains a copy of the selected balloon and the few actions associated to it
Here is the road map :
Detect a long press in the collection view cells
Copy the selected balloon and present it inside a secondary view controller (ActionVC)
Adjust the position of the selected balloon inside the ActionVC. If the selected balloon is under the future action button, it should be moved. If the selected balloon does not bother anyone, it should be presented without any change.
Adjust the content offset of the collection view to reflect 3. The modification should be done alongside 3 to look like if the cell was actually moved.
Detect any touch on the ActionVC
Dismiss the ActionVC
Here is an example project.
To copy the balloon, I actually create a view of the same class as the one used in the collection view cell. But you could use a snapshot.
To adjust the selected balloon position inside the ActionVC, I used constraints with priorities. One claims "don't be under the action button", another one claims "be exactly where the cell was". I used a simple coordinates conversion to compute the expected selected balloon position inside the ActionVC.
To perform 3 alongside 4, I used the transitionCoordinator of the ActionVC, but you could use a simple animation block and present the ActionVC without animation.
I'm sorry if this answer will not fulfill your request completely, but hope it will help you.
Your initial code was right, but you have not set Scrolling type. So I suggest you use this method selectRow(at:animated:scrollPosition:) Just set scroll position:
self.messagesTableView.selectRow(at: indexPath, animated: true, scrollPosition: UITableViewScrollPosition.top)
This also will select this row and hence call the following method tableView(_:didSelectRowAt:). So you will need to implement highlighting logic here:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// your logic of changing color
}
Then you should implement similar action, but to deselect the row using deselectRow(at:animated:)I believe this should be done when user finished his action. Also you need to implement similar function tableView(_:didDeselectRowAt:) to return color back:
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
// change color back
}
Update:
You may also use the setHighlighted(_:animated:) method to highlight a cell. In this way you may avoid using selectRowAt/didSelectRowAt/didDeselectRowAt and scroll tableView using
tableView?.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true).
There are a lot of question with the same title but somehow I felt that every case is different as my problem is still not resolved and I've no clue why and also there are not enough solutions for Swift. So here is my scenerio:
I've used CarbonKit to show 4 view controllers in a single view controller. The FourthViewController sends a get call using AlamoFire. It successfully loads the data and reload tableView. However when I drag this table view my app crashes and I get EXC_BREAKPOINT and sometimes EXC_BAD_ACCESS for the same thing.
Here is what I've tried:
Upon searching I came to know about zombies. So I enabled it and when the app gives me EXC I get this:
-[CALayer removeAllAnimations]: message sent to deallocated instance
So as I checked there was no animation I might have been using in this class. But considering it's inside CarbonKit they might have something to do with it but I'm unable to figure it out.
I run Product > Analyze from xCode and the most of the blue icons are on FBSDK which have nothing to do with this class so it was not useful to debug at all.
Here are the screenshots:
and
and here is what Instruments 9.0 zombies message gave me:
and
I get this in console of xCode:
If you need more information I'll update my questions.
UPDATE
Here is my code when I'm getting the data from web using Alamofire. Now this call runs successfully and I can scroll down to view all the data that is loaded in the table. But when i scroll up then it crashes with the above errors.
let headers = [
"security-token": "MY_SECURITY_CODE_HERE",
"Content-Type": "application/x-www-form-urlencoded"
]
let url = "URL_GOES_HERE"
//print(url)
Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
if let result = response.result.value as? [String: AnyObject] {
switch response.result {
case .success:
if result["status"] as! Int == 0
{
self.clearAllNotice()
let alert = UIAlertController(title: "Error",
message: "\(result["msg"] ?? "Our Server is not responding at the moment. Try again later!" as AnyObject)",
preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK",
style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
else
{
self.clearAllNotice()
for docReviewDict in (result["data"] as! [AnyObject]) {
let docReview = DoctorReview(dict: docReviewDict as! [String : AnyObject])
self.mainDoctorReviewsArray.append(docReview)
}
self.tableView.reloadData()
}
case .failure(let error):
self.clearAllNotice()
let alert = UIAlertController(title: "Something is wrong",
message: error.localizedDescription,
preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK",
style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
} else {
print("Somethins is wrong with the URL")
}
}
Just in case here is the cell which is loaded in the tableview.
Latest:
Based on the comment I've done this:
DispatchQueue.main.async {
self.tableView.reloadData()
}
but still the same result I'am getting
Thread 1: EXC_BAD_ACCESS (code=1, address=0xb4c4beb8)
on let cell = tableView.dequeueReusableCell(withIdentifier: "ReviewCell") as! ReviewsTableViewCell line when I drag down on table view.
IMPLEMENTATION OF CELL
The class name is ReviewsViewController which has a #IBOutlet fileprivate var tableView: UITableView!.
Then in viewDidLoad():
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
Then I've table view delegate and datasource:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainDoctorReviewsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReviewCell") as! ReviewsTableViewCell //WHEN THE APP IS CRASHED AS I DRAG THE TABLE VIEW IT CRASH ON THIS LINE. As this line is highlighted in red with message "Thread 1: EXC_BREAKPOINT (code=1, subcode=0x186ddd61c)"
cell.userPhoto.layer.cornerRadius = cell.userPhoto.frame.width / 2;
cell.userPhoto.layer.masksToBounds = true
cell.userName.text = mainDoctorReviewsArray[indexPath.row].name
cell.starRating.settings.fillMode = .precise
if let rate = mainDoctorReviewsArray[indexPath.row].rating
{
cell.starRating.rating = Double(rate)!
}
else {
cell.starRating.rating = 0
}
cell.desc.text = mainDoctorReviewsArray[indexPath.row].feedback
cell.dateOfReview.text = mainDoctorReviewsArray[indexPath.row].dates
cell.timeOfReview.text = mainDoctorReviewsArray[indexPath.row].times
return cell
}
and in the cell class there is just:
import UIKit
import Cosmos
class ReviewsTableViewCell: UITableViewCell {
#IBOutlet var timeOfReview: UILabel!
#IBOutlet var dateOfReview: UILabel!
#IBOutlet var desc: UITextView!
#IBOutlet var starRating: CosmosView!
#IBOutlet var userName: UILabel!
#IBOutlet var userPhoto: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
The screenshot of tableView is above in this question.
This is the parent class under which 4 ViewControllers loads: dropbox.com/s/d5sh4ej1ssv6873/… And this is the class which has the tableview is and on scroll it crashed the app: dropbox.com/s/3kiju9hn3uewzar/ReviewsViewController.swift?dl=0
As someone mentioned above it probably is related with working with ui objects not from main thread.
If your using xCode 9 you should see as a runtime warning. (You can set checkmark in EditScheme->Diagnostics->MainThreadChecker->pause on issues).
Also you can override dealloc method of you ReviewsTableViewCell and add some logs\asserts to check if it's called from main thread.
Usually it happens if you pass your view\cell to some block, and you will have this crash on releasing this object when exiting from block.
I'm gonna make some small suggestions to clean up your code. Not sure if it will solve the problem but it will help to ensure that your issue isn't based on something trivial.
I'm not sure where you're calling it, but in the request load completion handler make sure to capture self weakly.
Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { [weak self] response in
This way you're not forcing the object to stay alive until the loading finishes, which may cause it to load in the wrong place.
Make sure that the completion block (mentioned above) is running on the main thread. You're making calls to UI, so just check it to be safe.
Your IB outlets should be declared as #IBOutlet weak var foo: UIView? You might be causing yourself problems by strongly referencing the views.
dequeueReusableCell(withReuseIdentifier:) can return nil. You should be using dequeueReusableCell(withReuseIdentifier:for:) - and don't forget to register the cell using register(_:forCellWithReuseIdentifier:) first.
Did you register the cell class type?
If not add in viewDidLoad:
self.tableView.register(ReviewsTableViewCell.self, forCellReuseIdentifier: "ReviewCell")
Next just dequeue it in cellForRow:
let cell = tableView.dequeueReusableCellFor(identifier: "ReviewCell", indexPath: indexPath) as! ReviewsTableViewCell
Check if the error is still happening
In my case, this happened when I executed this (myView is a subview on a cell)
myView.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
I don't find anything removing a layer from your code but if you did it outside of that, then I think that's the problem.
I have UITableView with about 20 row which each contain a UITextField. The first time I tap in a textfield will open the keyboard and I am ready to edit this textfield. If I tap on the next textfield (notice the keyboard is displayed all the time) the keyboard is still displayed but the blue cursor is not in the new textfield and I cannot enter any text. But if I tap on another textfield again, it works just fine. This behavior occurs alternately, one time it works the other time it doesn't.
The delegate method textFieldShouldBeginEditing(_:) is always called, wether I can edit or not. The delegate method textFieldDidBeginEditing(_:) is only called when editing works.
This is the code for cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldCell")!
let titleLabel = cell.viewWithTag(1) as! UILabel
let contentTextField = cell.viewWithTag(2) as! FixableTextField
contentTextField.delegate = self
contentTextField.inputAccessoryView = doneToolbar
// Enable/disable editing for text fields
if isEditing {
contentTextField.enableEditing()
} else {
contentTextField.disableEditing()
}
// Present Profile Data
if profileUpdateBuffer != nil {
switch indexPath.row {
case 0:
titleLabel.text = "Count"
contentTextField.text = "\(profileUpdateBuffer!.count)"
contentTextField.purposeID = "count"
contentTextField.keyboardType = .numberPad
case 1:
titleLabel.text = "City"
contentTextField.text = "\(profileUpdateBuffer!.city)"
contentTextField.purposeID = "city"
contentTextField.keyboardType = .default
// ...
case 20:
titleLabel.text = "Name"
contentTextField.text = "\(profileUpdateBuffer!.name)"
contentTextField.purposeID = "name"
contentTextField.keyboardType = .default
default:
titleLabel.text = ""
contentTextField.text = ""
}
return cell
}
// No data available -> show info in first row
else {
if indexPath.row == 0 {
titleLabel.text = "No data"
contentTextField.text = "No data"
}
else {
titleLabel.text = ""
contentTextField.text = ""
}
return cell
}
}
The enableEditing() and disableEditing() method are from class FixableTextField. I can see that the textfields are always enabled because I can see the textfield border
// Extract from FixableTextField class
func enableEditing() {
self.isEnabled = true
self.borderStyle = .roundedRect
}
func disableEditing() {
self.isEnabled = false
self.borderStyle = .none
}
Code for the UITextField
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// Delete empty field indicator "-"
if textField.text == "-" {
textField.text = ""
}
//Move profileTable's contentView to correct position
if textField is FixableTextField {
let path = IndexPath(row: rowMap[(textField as! FixableTextField).purposeID]!, section: 0)
moveContentViewUp(indexPath: path)
}
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
// Save new value to profileUpdateBuffer
do {
try self.profileUpdateBuffer?.setProperty(value: textField.text!, key: (textField as! FixableTextField).purposeID)
} catch ProfileError.PropertySettingWrongType {
let falseInputAlert = UIAlertController(title: "False Input", message: "The input for this field is not valid.", preferredStyle: .alert)
falseInputAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(falseInputAlert, animated: true, completion: nil)
} catch {
print("Error when trying to set property for profileUpdateBuffer in ProfileViewController")
}
// Display new data in table
profileTable.reloadData()
}
Extract from setProperty method which is from class ProfileData. profileUpdateBuffer is of type ProfileData
func setProperty(value:String, key:String) throws {
switch key {
case "count":
count = value
case "city":
count = value
// ...
case "name":
name = value
default:
throw ProfileError.PropertySettingWrongType
}
}
I've made a small program to mimic the behavior you describe.
It seems the issue is caused by table view data reloading at the end of your textFieldDidEndEditing(_:):
func textFieldDidEndEditing(_ textField: UITextField) {
// Save new value to profileUpdateBuffer
do {
try self.profileUpdateBuffer?.setProperty(value: textField.text!, key: (textField as! FixableTextField).purposeID)
} catch ProfileError.PropertySettingWrongType {
let falseInputAlert = UIAlertController(title: "False Input", message: "The input for this field is not valid.", preferredStyle: .alert)
falseInputAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(falseInputAlert, animated: true, completion: nil)
} catch {
print("Error when trying to set property for profileUpdateBuffer in ProfileViewController")
}
// Display new data in table
profileTable.reloadData()
}
Try removing profileTable.reloadData() for the sake of experiment to confirm the root cause of the problem (yes, your other cells will not be updated).
One way to solve this is by utilizing direct cell updates on visibleCells in textFieldDidEndEditing(_:). I see profileUpdateBuffer? is your data model. Just update your cell's titleLabel and textField properties manually from your model if they are in visible cells property of the table view.
If you want to size the cells accordingly, use AutoLayout and UITableViewAutomaticDimension for table view row height combined with beginUpdates()/endUpdates() calls.
For more details on how to achieve direct cell manipulation and/or dynamic cell size update without loosing the keyboard focus check the accepted answer on this question I've already answered.
Hope this will help!
I believe the problem is that you call profileTable.reloadData()... You should probably only reload one cell. Perhaps textFieldDidEndEditing(:) gets called with the old TextField as a parameter, followed by textFieldShouldBeginEditing(:) for the new TextField. The problem is that you refresh all the cells at the end of textFieldDidEndEditing(:), which means the TextField passed by the system as a parameter to textFieldShouldBeginEditing(:) may not necessary be the same one that the corresponding cell contains... It may be that those new key-strokes get sent to a TextField belonging to a cell that is in the Queue of reusable cells, i.e. not visible, but still existing somewhere in memory.
I want to remove cell of table in Apple watch. Is there any delegate method like commitEditingStyle which we use in iPhone app. Or any user interface like gesture or any thing so i can remove cell and data which i want at runtime. I already use didSelectRowAtIndex for navigate to other controller so please give me a other way.So, i can maintain both events. Thank you in advance.
There is no way. Context menu could be used but it works for full controller, and there is no way to detect row/coordinates. The best way when user clicks on row you display detail controller with delete button or context menu
for Swift 5
If you are looking to delete one of the rows, you can do this.
add a remove row function.
add the didSelectRowAt function
add an alert function to the didSelectRowAt
then add the remove function to the alert
also add a cancel button to the alert.
func removeRow(at row: Int) {
array.remove(at: row)
self.loadTable()
}
override func table(_ table: WKInterfaceTable, didSelectRowAt rowIndex: Int) {
let delete = WKAlertAction(title: "Delete Item", style: .default) {
self.removeRow(at: rowIndex)
self.userDefaults.setValue(self.array, forKey: "items")
}
let cancel = WKAlertAction(title: "cancel", style: .cancel) {}
self.presentAlert(withTitle: "Alert", message: "Do you want to delete this item?", preferredStyle: .alert, actions: [delete,cancel])
}