草庐IT

ios - 滚动 UITableView 时的值变化

coder 2023-07-16 原文

我有一个带有按钮的 UITableView,该按钮根据用户是否“收藏”帖子进行切换。一切正常,除了滚动表格 View 时,按钮会发生变化。这是我的代码:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    guard let feed = self.feed else {
        return 0
    }
    return feed.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if feed!.count > 9 {
        if indexPath.row == feed!.count - 1 {
            self.loadMorePosts()
        }
    }

    if hasImageAtIndexPath(indexPath) {
        return imageCellAtIndexPath(indexPath)

    } else {
        return basicCellAtIndexPath(indexPath)
    }

}

func hasImageAtIndexPath(indexPath:NSIndexPath) -> Bool {
    let post = self.feed?[indexPath.row]

    if post?.image?.isEmpty == false {
        return true
    }

    return false
}

func imageCellAtIndexPath(indexPath:NSIndexPath) -> PostCellImage {
    let cell:PostCellImage = self.tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! PostCellImage


    if let post = self.feed?[indexPath.row] {
        let likedPost = post.hasFavorited

        if likedPost == true {
            if let favoriteCount = post.favoriteCount {
                let count = String(favoriteCount)
                cell.likeButton.setTitle(count, forState: .Normal)
                cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal)
                cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal)
                cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
                cell.likeButton.tag = post.id!
            }
        } else {
            if let favoriteCount = post.favoriteCount {
                let count = String(favoriteCount)
                cell.likeButton.setTitle(count, forState: .Normal)
                cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
                cell.likeButton.tag = post.id!
            }
        }
    }

    return cell
}

收藏的帖子数组

var favoritedPosts =  [Int]()

表格 View

if let likedPost = post.hasFavorited {
    if likedPost == true {
        self.favoritedPosts.append(indexPath.row)             
        print(self.favoritedPosts)
    }
}

if self.favoritedPosts.contains(indexPath.row) {
    let count = String(post.favoriteCount)
    cell.likeButton.setTitle(count, forState: .Normal)
    cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal)
    cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal)
    cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
    cell.likeButton.tag = post.id!     
} else {
    let count = String(post.favoriteCount)
    cell.likeButton.setTitle(count, forState: .Normal)
    cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
    cell.likeButton.tag = post.id!        
}  

最佳答案

在您的 UITableViewCell PostCellImage 子类中,您应该覆盖 prepeareForReuse 函数 - 将单元格转换为默认模式。

swift :

override func prepareForReuse() {
    super.prepareForReuse()

    //set cell to initial state here 
    //set like button to initial state - title, font, color, etc.
}

关于ios - 滚动 UITableView 时的值变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402313/

有关ios - 滚动 UITableView 时的值变化的更多相关文章

  1. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  2. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. ruby-on-rails - 启用 Rack::Deflater 时 ETag 发生变化 - 2

    在启用Rack::Deflater来gzip我的响应主体时偶然发现了一些奇怪的东西。也许我遗漏了一些东西,但启用此功能后,响应被压缩,但是资源的ETag在每个请求上都会发生变化。这会强制应用程序每次都响应,而不是发送304。这在没有启用Rack::Deflater的情况下有效,我已经验证页面源没有改变。我正在运行一个使用thin作为Web服务器的Rails应用程序。Gemfile.lockhttps://gist.github.com/2510816有没有什么方法可以让我从Rack中间件获得更多的输出,这样我就可以看到发生了什么?提前致谢。 最佳答案

  7. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  8. ruby-on-rails - 如何从过时的 TZInfo 标识符中获取 Rails TimeZone 名称? - 2

    已经有一个问题回答了如何将“America/Los_Angeles”转换为“PacificTime(US&Canada)”。但是我想将“美国/太平洋”和其他过时的时区转换为RailsTimeZone。我无法在图书馆中找到任何可以帮助我完成此任务的东西。 最佳答案 来自RailsActiveSupport::TimeZonedocs:TheversionofTZInfobundledwithActiveSupportonlyincludesthedefinitionsnecessarytosupportthezonesdefinedb

  9. ruby - 获取数组中的值并最小化某个类属性的最优雅的方法是什么? - 2

    假设我有以下类(class):classPersondefinitialize(name,age)@name=name@age=ageenddefget_agereturn@ageendend我有一组Person对象。是否有一种简洁的、类似于Ruby的方法来获取最小(或最大)年龄的人?如何根据它对它们进行排序? 最佳答案 这样做会:people_array.min_by(&:get_age)people_array.max_by(&:get_age)people_array.sort_by(&:get_age)

  10. ruby-on-rails - 使用作为方法的值在 ruby​​ 中搜索哈希 - 2

    我在搜索我的值是方法的散列时遇到问题。我只是不想运行plan_type与键匹配的方法。defmethod(plan_type,plan,user){foo:plan_is_foo(plan,user),bar:plan_is_bar(plan,user),waa:plan_is_waa(plan,user),har:plan_is_har(user)}[plan_type]end目前如果我传入“bar”作为plan_type,所有方法都会运行,我怎么能只运行plan_is_bar方法呢? 最佳答案 这个变体怎么样?defmethod

随机推荐