Post

Replies

Boosts

Views

Activity

Unselect cell in tableview if the cell is visible
In a tableview, the multiple selection is set in the storyboard. The following code is used to unselect a cell: tableView.deselectRow(at: indexPath, animated: true) It works fine if the cell is not visible. If the cell is visible, the selection check mark is gone. However, the cell seems still to be in selection mode, and it cannot be selected again. Here is more information for my case: I have a tabview with 2 tabs. One is for editing data, and the other is for displaying data in a tableview. n the second tabview, I made selections of cells. Then switch to the first tabview of the data source. If there is any change in the data source, I'll set a flag about the change. In the second tabview, I'll clear any selection if the flag is on. override open func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) guard isDatasourceChanged else { return } let selRows = tableView.indexPathsForVisibleRows ?? [] for ip in selRows { tableView.deselectRow(at: ip, animated: true) } } For example, here is the cell is selected: I changed the datasource tabview first, and then back to the second tabview. The viewWillAppear event is called and all selections are set to deselected. The visible cell looks like this: the selection check mark is gone, but it looks like it is still highlighted and cannot be selected again. I verified that the hi lighted cell is not in selected cell, but I cannot select the cell again. How can I set the cell unselected and not hilighed if it is visible?
1
0
883
Aug ’22
A Problem with Plural Localization
I have a function to get the localized string for plural cases. extension String static func localizedStringForPlurals( formatKey key: String, comment: String = "", args: CVarArg... ) -> String { let format = NSLocalizedString(key, comment: comment) var result = withVaList(args){ (NSString(format: format, locale: NSLocale.current, arguments: $0) as String) } return result } } The key point is NSLocalizedString to get localized string for plural case from dict file. I have an example of getting localized string for taps. If the int number is one, the localized string is "This tap"; if int is larger than one, i.e., others, the string is "These taps". See attachment dict file (English). It works fine for English. For example: var count: Int ... let locS = String.localizedStringForPlurals(             formatKey: "theseTaps", args: count) // "This tap" if count is 1 // "These taps" if count is > 1 However, it does not work well for Chinese. Here is the similar dict file for Chinese. The result in Swift code is always plural string, i.e., "These tap", even count is 1. I am not sure if this is a bug in NSLocalizedString or not. I know that in Chinese in general there are no plural cases. However, as in this example, there are plural cases, this tap or these tap. In Chinese there is no plural for "tap", but "this", and "these" in Chinese are different, and they are plural cases. Any way to resolve the issue? I do like Apple's localization for plural framework. I would like this framework works as developer's expectation, as in my example in two dict files.
2
0
1.6k
Aug ’22
How to detect my app back from lock/inactive status back to active?
I have an app with a tab view on the bottom. The tab view has its color set when a main view controller is displayed. Browse into other views by using the navigation control and back. It displays the tab bar in the right color as normal. The problem is that when the screen is locked or the user switches to another app and then back to my app (unlock screen or switch back to my app), the tab view loses its color and looks transparent. This makes other inactive tabs hard to see. The main table view looks OK and keeps its color. Just the tab bar becomes transparent. I have a class called GradientViewController as a base class for defining colors for the table view and navigation bar. class GradientViewController: UIViewController { func gradientView_UpdateBackgroundColors() { let tv = getTableView() // This call works fine to set table view color gvHelper.updateBackgroundColorsFor( backgroundView1: tv, backgroundView2: view) gvHelper.updateNavigationBarColors( navigationController) } ... override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) gradientView_UpdateBackgroundColors() } } Here is my helper class to update the navigation bar: class GradientViewHelper { ... func updateNavigationBarColors( _ nv: UINavigationController?) { guard let nv = nv else { return } let bar = nv.navigationBar // top and bottom colors are UIColor type and initialized at init bar.barTintColor = colorTop bar.backgroundColor = colorTop if let bar = nv.toolbar { bar.barTintColor = colorBottom bar.backgroundColor = colorBottom } } Not sure what is missing? One way to fix this issue is to catch the event when my app is back in focus from the lock screen or back from other apps, and set up the bottom color. I'm not sure what event I could catch. The attached images are snapshots of the problem.
0
0
570
Aug ’22
Call Swift functions from JS
I have a VIewController with WKWebView to display HTML content to my users. What I need is to get some information from my project side(swift codes) such as app version, data reports to javascrtript side. On the side of js, I'll be able to generate html codes or display elements based on swift function calls. Here are some codes in my ViewController: class AboutViewController: UIViewController { @IBOutlet weak var webView: WKWebView! { didSet { setJS() } } private let jsCtx = JSContext() private func setJS() { let obj = MyVersionClass() jsCtx.ctx.setObject(             obj,             forKeyedSubscript: "versionObj") } override func viewDidLoad() {         super.viewDidLoad() // build url webView.loadFileURL(             url,             allowingReadAccessTo: url) let request = URLRequest(url: url)         webView.load(request) } MyVersionClass is defined as: import JavaScriptCore @objc protocol JSAppVersionProtocol: JSExport {     func getAppVersion() -> String     static func createObj() -> MyVersionClass } class MyVersionClass: NSObject, JSAppVersionProtocol {     static func createObj() -> MyVersionClass {         let obj = MyVersionClass()         return obj     }     func getAppVersion() -> String {        ...     } } The class AboutViewController will load an html file with js defined in script section <head> <script type="text/javascript" src="../version.js"></script> </head> <body onload="updateVersion()"> ... <span id="appVersion">To be updated with my app version      </span> .... JS code: function updateVersion() {     let e = document.getElementById("appVersion");     var ver =versionObj.getAppVersion(); // another try, see following codes // var ver =versionObj.createObj().getAppVersion();     e.innerHTML = ver; } I tested the js function call in my AbountViewController class right after I setObject like this: let result = jsCtx.evaluateScript("versionObj") print("\(result)" I got the result in console like this: <MyApp.JSAppVersion: 0x282aa0af0> I also tried to setObject like this: jsCtx.ctx.setObject(             MyVersionClass.self,             forKeyedSubscript: "versionType") // My test of script function let result = jsCtx.evaluateScript("versionType") print("\(result)" // result is <MyApp.MyVersionClass> However, it seems that my js does not know what my swift function code is. It fails to get the app version. Not sure what is missing or wrong? How can I set up mu swift function available in js side?
2
0
4k
Aug ’22
How to add WebKit Entitlement
I have an iOS native app. I have added WKWebView to view controller to show html content. class MyHTML1ViewController: UIViewController { private var webView: WKWebView! var htmlFile: String = "" // default value, can be changed override func viewDidLoad() { super.viewDidLoad() let url = Bundle.main.url( forResource: htmlFile, withExtension: "html") let request = URLRequest(url: url) webViewBase.load(request) } ... } I run the app on my iPhone through Xcode. It displays html content fine. However, on closing the view, I saw the following error messages in console: ProcessAssertion: Failed to acquire RBS assertion 'WebProcess Suspended Assertion' for process with PID=91330, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}> Should I add webkit entitlement? how can I do it in Xcode? I prefer to get rid of this error message if they are just warning in console, does not causing app crash. My Xcode is Version 13.4.1.
3
1
5.1k
Aug ’22