2016-05-06 18 views
0

自分のお気に入りのスポットを投稿、編集、削除することができるログイン機能を備えたアプリを作っています。ユーザー。自分で投稿した投稿のみにuitableviewcellの編集ボタンを表示する方法

自分が投稿した投稿にのみ表示され、他のユーザーの投稿には表示されない編集ボタンを実装しようとしています。

私はUITableviewCellクラスの 'configureCell関数'と呼ばれるFeedViewControllerを持っています。 (:UITableViewCellのクラスPostCell):

func configureCell(post: Post, img: UIImage?, img2: UIImage?) { 
    self.post = post 
    likeRef = DataService.ds.REF_USER_CURRENT.childByAppendingPath("likes").childByAppendingPath(post.postKey) 

    self.descriptionText.text = post.postDescription 
    self.descriptionText.scrollRangeToVisible(NSMakeRange(0, 0)) 
    self.likesLbl.text = "\(post.likes)" 
    self.postTitle.text = post.postTitle 
    self.postLocation.text = post.postLocation 
    self.username.text = post.username 
    self.postKeyLbl.text = post.key 

    if post.postImgUrl != nil { 
     if img != nil { 
      self.showcaseImg.image = img 
     } else { 
      request = Alamofire.request(.GET, post.postImgUrl!).validate(contentType: ["image/*"]).response(completionHandler: { request, response, data, err in 
       if err == nil { 
        let _img = UIImage(data: data!)! 
        self.showcaseImg.image = img 
        FeedVC.imageCache.setObject(_img, forKey: self.post.postImgUrl!) 
       } else { 
        print(err.debugDescription) 
       } 
      }) 
     } 
    } else { 
     self.showcaseImg.hidden = true 
    } 

    if post.userImgUrl != nil { 
     if img2 != nil { 
      self.profileImg.image = img2 
     } else { 
      request = Alamofire.request(.GET, post.userImgUrl!).validate(contentType: ["image/*"]).response(completionHandler: { request, response, data, err in 
       if err == nil { 
        let _img2 = UIImage(data: data!)! 
        self.profileImg.image = img2 
        FeedVC.imageCache.setObject(_img2, forKey: self.post.userImgUrl!) 
       } else { 
        print(err.debugDescription) 
       } 
      }) 
     } 
    } else { 
     print("no image") 
    } 

    likeRef.observeSingleEventOfType(.Value, withBlock: { snapshot in 
     if snapshot.value is NSNull { 
      self.likesImg.image = UIImage(named: "heart") 
     } else { 
      self.likesImg.image = UIImage(named: "heart-filled") 
     } 
    }) 

    let getUid = NSUserDefaults.standardUserDefaults().valueForKey(KEY_UID) 
    if String(getUid!) == (self.post.postUid) { 
     editBtn.hidden = false 
     delBtn.hidden = false 
    } 

}それは最後の部分についてです

これは私のUITableViewCellのコードがある

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell { 
     let postList = searchController.active ? searchResult[indexPath.row] : posts[indexPath.row] 
     let post = postList 
     cell.request?.cancel() 

     var image: UIImage? 
     if let url = post.postImgUrl { 
      image = FeedVC.imageCache.objectForKey(url) as? UIImage 
     } 

     var image2: UIImage? 
     if let url2 = post.userImgUrl { 
      image2 = FeedVC.imageCache.objectForKey(url2) as? UIImage 
     } 

     cell.configureCell(post, img: image, img2: image2) 

     return cell 
    } else { 
     return PostCell() 
    } 
} 

:これはFeedViewControllerのコードの一部です:

let getUid = NSUserDefaults.standardUserDefaults().valueForKey(KEY_UID) 
    if String(getUid!) == (self.post.postUid) { 
     editBtn.hidden = false 
     delBtn.hidden = false 

}

その部分は機能しません。編集(および削除)ボタンは、特定のユーザーの投稿だけでなく、他のユーザーの投稿の一部にも表示されます。私は何が間違っているのか分からない。たぶん私は 'ソートデータ関数'を実装しています。ここでは、「日付」または「好き」の投稿をソートしています。ポストが再シャッフルすると、他のユーザーのランダムなセルに多くの編集ボタンが表示されます。

しかし、私は本当に知りません。私は誰かが私を助けることができることを願っています!もっとコードが必要な場合はお知らせください:-)たくさんありがとう!それが関連付けられているので、あなたが、細胞、ボタンが隠されないことがあり、細胞の一部を再利用しているため

if String(getUid!) == (self.post.postUid) { 
    editBtn.hidden = false 
    delBtn.hidden = false 
} else { 
    editBtn.hidden = true 
    delBtn.hidden = true 
} 

敬具、 Dide

答えて

1

はこれにelse節を追加します。あるユーザの投稿を、別のユーザの別の投稿に再利用することができる。この状況を扱うelse節がないので、最初にfalseに設定された 'hidden'プロパティを持つボタンは、postUidとgetUidが一致しなくても、表示されません。お役に立てれば!

+0

これは機能します。だから愚かな私はこれを早く試していない!どうもありがとう!! – debbiedowner

関連する問題