首页 \ 问答 \ 滚动时TableView重新排列/创建空白单元格。(TableView rearranging / creating blank cells when scrolling. Swift)

滚动时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()检索vars resultsnumberofContacts的情况下,该部分工作正常。 该表将填充所有联系人,您可以完美地向下滚动它们,但是一旦单元格滚动到屏幕外,您向后滚动的值将被重新分配。

我还添加了一个功能,当您点击联系人姓名时,您可以调用他们的第一个指定号码,如下所示:

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 and numberofContacts are being retrieved by the CNContactStore(), 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
更新时间:2022-07-14 12:07

最满意答案

获取父目录,直到到达根目录

如果这是一条绝对的道路

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:\ *

相关问答

更多
  • 我过去曾经使用过这样的东西: 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 ...
  • 获取父目录,直到到达根目录 如果这是一条绝对的道路 private static IEnumerable SplitPath(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 ...
  • 好! 好! 我能够使用以下行来完成它: 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)
  • 您可以使用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 ...
  • 你能试试吗? 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)