滚动时TableView重新排列/创建空白单元格。(TableView rearranging / creating blank cells when scrolling. Swift)
好吧,所以我正在创建这个视图,我将所有联系人都带到手机上,我只是将它们添加到一个数组中并循环通过该数组,以便在我的表视图中创建单元格。 问题是,当我滚动时,单元格内容在索引上更改或者只是从表中消失。 我正在创建我的表并像这样填充它:
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return Int(numberofContacts) } var i = 0 public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print(i) let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") if(i < numberofContacts){ cell.textLabel?.text = results[i].givenName + " " + results[i].familyName } i++ return cell }
在
CNContactStore()
检索varsresults
和numberofContacts
的情况下,该部分工作正常。 该表将填充所有联系人,您可以完美地向下滚动它们,但是一旦单元格滚动到屏幕外,您向后滚动的值将被重新分配。我还添加了一个功能,当您点击联系人姓名时,您可以调用他们的第一个指定号码,如下所示:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { if results[indexPath.row].isKeyAvailable(CNContactPhoneNumbersKey){ let selected = (results[indexPath.row].phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String let urlPhone = NSURL(string: "tel://\(selected)") UIApplication.sharedApplication().openURL(urlPhone!) } }
请帮忙,我不知道我在哪里弄乱这个。 谢谢。
Alright, so I'm creating this view where I bring all the contacts on the phone, and I'm simply adding them in an array and looping trough that array in order to create cells in my table view. The problem is, when I scroll, the cell content changes on index or simply disappears from the table. I'm creating my table and populating it like so:
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return Int(numberofContacts) } var i = 0 public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print(i) let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") if(i < numberofContacts){ cell.textLabel?.text = results[i].givenName + " " + results[i].familyName } i++ return cell }
Where the vars
results
andnumberofContacts
are being retrieved by theCNContactStore()
, that part works fine. The table gets populated with all the contacts and you can scroll them down perfectly, but once the cell get's scrolled out of the screen, the value when you scroll back gets reassigned.I'm also adding the feature of when you click a contact name you call their first assigned number like so:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { if results[indexPath.row].isKeyAvailable(CNContactPhoneNumbersKey){ let selected = (results[indexPath.row].phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String let urlPhone = NSURL(string: "tel://\(selected)") UIApplication.sharedApplication().openURL(urlPhone!) } }
Please help, I have no idea where I'm messing this. Thank you.
原文:https://stackoverflow.com/questions/35162570
最满意答案
获取父目录,直到到达根目录
如果这是一条绝对的道路
private static IEnumerable<string> SplitPath(string path){ do{ yield return path; path=Path.GetDirectoryName(path); } while(path!=null); }
如果它是一个相对路径
private static IEnumerable<string> SplitRelativePath(string path){ do{ yield return path; var lastIndex=path.LastIndexOf('\\'); if(lastIndex==-1) yield break; path=path.Substring(0, lastIndex); } while(path!=null); } //usage SplitRelativePath(@"Customer\Calls\A1\A2\A3\A4"); /* result: C:\Customer\Calls\A1\A2\A3\A4 C:\Customer\Calls\A1\A2\A3 C:\Customer\Calls\A1\A2 C:\Customer\Calls\A1 C:\Customer\Calls C:\Customer C:\ *
Get parent directory until you get to the root
if it's an absolute path
private static IEnumerable<string> SplitPath(string path){ do{ yield return path; path=Path.GetDirectoryName(path); } while(path!=null); }
if it is a relative path
private static IEnumerable<string> SplitRelativePath(string path){ do{ yield return path; var lastIndex=path.LastIndexOf('\\'); if(lastIndex==-1) yield break; path=path.Substring(0, lastIndex); } while(path!=null); } //usage SplitRelativePath(@"Customer\Calls\A1\A2\A3\A4"); /* result: C:\Customer\Calls\A1\A2\A3\A4 C:\Customer\Calls\A1\A2\A3 C:\Customer\Calls\A1\A2 C:\Customer\Calls\A1 C:\Customer\Calls C:\Customer C:\ *
相关问答
更多-
拆分可执行路径和参数(Split executable path and arguments)[2023-04-24]
我过去曾经使用过这样的东西: char* lpCmdLine = ...; char* lpArgs = lpCmdLine; // skip leading spaces while(isspace(*lpArgs)) lpArgs++; if(*lpArgs == '\"') { // executable is quoted; skip to first space after matching quote lpArgs++; int quotes = 1; w ... -
如何将路径拆分为路径集(How to split path to set of Path)[2022-05-16]
获取父目录,直到到达根目录 如果这是一条绝对的道路 private static IEnumerableSplitPath(string path){ do{ yield return path; path=Path.GetDirectoryName(path); } while(path!=null); } 如果它是一个相对路径 private static IEnumerable SplitRelativePath(st ... -
str.partition和rpartition将做你想要的: def ssh_splitter(ssh_connect_string): user_host, _, path = ssh_connect_string.partition(':') user, _, host = user_host.rpartition('@') return user, host, path print(ssh_splitter('user@132.243.32.14:/random/file/ ...
-
正如您所发现的, SVGPathElement具有逐步遍历路径段的方法。 你只需要逐步完成它们,直到找到你必须细分的那个。 请注意,这些API正处于变化之中。 因此,您可能需要在某些浏览器中使用polyfill - 这取决于您是选择使用新API还是旧API 唯一难以细分的路径段类型是二次和三次贝塞尔曲线。 但使用De Casteljau的算法,这些实际上也很简单。 请参阅:将贝塞尔曲线分为两个相等的一半 该答案解释了划分立方贝塞尔曲线。 然而,二次版本非常相似。 As you have discovered ...
-
使用os.path.join构造目标文件的路径: import os.path [...] outputFilename = os.path.join(destination_directory, "page%s.pdf" % i) with open (outputFilename, "wb") as outputStream: output.write(outputStream) 您可以使用..作为父目录, /作为根目录 use os.path.join to construct a pa ...
-
因此,根据您的查询,数字: 59, 83 and 90是这些行的常量,如果是,您可以尝试这样: SELECT t1.Attributes as device, t2.Attributes as Mac_Address , t3.Attributes as "user" from (SELECT Device_Index, Attributes FROM [dbo].[DeviceAttributes] WHERE Attribute_index= 59) t1 INNER JOIN (SELEC ...
-
分路径powershell(Split-path powershell)[2021-12-10]
好! 好! 我能够使用以下行来完成它: Split-Path (Split-path $MyInvocation.InvocationName -Parent) well! well! I was able to do it using following line: Split-Path (Split-path $MyInvocation.InvocationName -Parent) -
Python:拆分硬编码路径(Python: split hard coded path)[2023-08-08]
您可以使用split功能两次: os.path.split(os.path.split(a)[0])[0] 这是有效的,因为os.path.split()返回一个包含头部和尾部两个项目的元组,并且通过取[0] ,我们将获得头部。 然后再次拆分并再次使用[0]取出第一个项目。 或者将您的路径与父目录连接两次: os.path.abspath(os.path.join(a, '..', '..')) 您可以轻松创建一个功能,可以根据需要退回多个步骤: def path_split(path, steps): ... -
我知道的唯一特殊情况是 文件名末尾 最后一个/ 文件夹名称为ae (可能或可能不在最后) 象征性的联系 Windows路径(使用\但我认为它不适用于您的情况) 用户输入错误,放多个/ 。 例如/tmp//something某些程序支持的/tmp//something 如果你的功能支持上述所有情况,我认为没问题。 The only special cases I know are with file name at the end with a / at the end with a folder name ...
-
拆分文件全名路径(Split File Fullname path)[2022-09-12]
你能试试吗? Name="Ficheros";Expression={$_.FullName -replace '.*?(\\Desktop.*)','$1'} 编辑:根据@TheMadTechnician的建议更新非贪婪的正则表达式 Can you try this? Name="Ficheros";Expression={$_.FullName -replace '.*?(\\Desktop.*)','$1'} EDIT: Updating for non-greedy regex as sugge ...