Swift - UITableView didSelectRowAtIndexPath & didDeselectRowAtIndexPath Add & Remove indexPath IDs
You could do this pretty simply by adding a Dictionary
property to your table view controller:
class ViewController : UITableViewController {
var selectedItems: [String: Bool] = [:]
// ...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = items.objectAtIndex(indexPath.row) as String
let itemId = selectedItem.componentsSeparatedByString("$%^")
// add to self.selectedItems
selectedItems[itemId[1]] = true
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = items.objectAtIndex(indexPath.row) as String
let itemId = selectedItem.componentsSeparatedByString("$%^")
// remove from self.selectedItems
selectedItems[itemId[1]] = nil
}
// can access the items as self.selectedItems.keys
func doSomething() {
for item in selectedItems.keys {
println(item)
}
}
}
Select and Deselect UITableView Rows
you can add a flag - hasSelected in CategoryDB
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
{
categoryList = categoryData[indexPath.row] as! CategoryDB
categoryList!.hasSelected = true
refreshLabel()
}
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == categoryTable
{
categoryList = categoryData[indexPath.row] as! CategoryDB
categoryList!.hasSelected = false
refreshLabel()
}
}
func refreshLabel(){
var label = ""
for object in categoryData as! CategoryDB {
if(object.hasSelected){
label += "\(categoryList!.category), "
}
}
categoryLabel.text! = label
}
UITableView - How to delete file while deleting cell?
Actually what you are doing you are removing the value from array at the top. You have to remove the value from array at the end of this function. As i have done in below code, please check my answer and use this code and then tell me if you face any issue. I Update my answer Please check it again.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( editingStyle== UITableViewCellEditingStyleDelete) {
currentcsvfile = [self.dirList objectAtIndex:indexPath.row];
csvfilenameSave = [NSString stringWithFormat:currentcsvfile];
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filePath = [docPath stringByAppendingPathComponent:csvfilenameSave];
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
[_dirList removeObjectAtIndex:indexPath.row];
[self.data beginUpdates];
[self.data deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.data endUpdates];
[data setEditing:NO animated:YES];
}
}
How to show a UITableViewCell as selected and still receive didDeselectRowAtIndexPath
Try to use these codes.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.isEditing) {
[cell setSelected:[self isCellFave:indexPath]];
[cell addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(unselectRow:)]];
}
}
}
Then add a tap gesture handler
- (void)unselectRow:(UITapGestureRecognizer *)sender {
UITableViewCell *cell = (UITableViewCell *) sender.view;
cell.selected = NO;
}
Swift UITableView didSelectRowAtIndexPath not getting called
You have to set an @IBOutlet
to the tableView
in you ViewController
and set as it's delegate
and dataSource
to you can see the data an respond to changes in the tableView
.
Something like this :
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
And implements the UITableViewDataSource
protocol too.
Or you can too in the Interface Builder set the ViewController
as it's delegate and dataSource
(more easy to do I think) and avoid to set manually in code like above. Is up to you.
I hope this help you.
UITableViewCell checkmark to be toggled on and off when tapped
Swift > 3.0
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .none
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
}
}
I solved by using two Swift functions: the didSelectRowAtIndexPath and the didDeselectRowAtIndexPath.
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .None
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .Checkmark
}
}
To make this work properly, add a line of code to your cellForRowAtIndexPath function to select a row when the table view is drawn on the screen, otherwise the didDeselectRowAtIndexPath will not be called the first time you select another row. Like so:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellData", forIndexPath: indexPath)
if (some condition to initially checkmark a row)
cell.accessoryType = .Checkmark
tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.Bottom)
} else {
cell.accessoryType = .None
}
return cell
}
Related Topics
Delete Image from Photo Gallery
Cocoa Pods Install on iOS Project Not Working
How to Load Thousands Records to Realm Correctly
iOS Sound Not Playing in Swift
Storing a Variable When the App Is First Installed
Swift iOS Record Video and Audio with Avfoundation
Array Element Checking in Swift
Xcode 6 Ignoring Breakpoints - Swift
How to Make App Wait Until Firebase Request Is Finished
How to Access Property or Method from a Variable
How to Update Particular Value of Child in Firebase Db
Post Parameter to Sever Using Dictionary Swift
How to Get Camera Calibration Data on iOS? Aka Avcameracalibrationdata
How Should Secure Transport Tls Be Used with Bsd Sockets in Swift