尝试在 iOS table 视图中实现 MGSwipeTableCell
Trying to implement MGSwipeTableCell in an iOS table view
我正在尝试在我的 Swift 2、Xcode 7 项目中实现可滑动的 table 视图。
我想使用这个库:
https://github.com/MortimerGoro/MGSwipeTableCell
我只是想找出将其实施到我的项目中的正确方法。
我有一个带有 TableView 的 UIView 以及一个自定义 TableViewCell
这里是每个 GitHub repo 的实现代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let reuseIdentifier = "programmaticCell"
var cell = self.table.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
if cell == nil
{
cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
}
cell.textLabel!.text = "Title"
cell.detailTextLabel!.text = "Detail text"
cell.delegate = self //optional
//configure left buttons
cell.leftButtons = [MGSwipeButton(title: "", icon: UIImage(named:"check.png"), backgroundColor: UIColor.greenColor())
,MGSwipeButton(title: "", icon: UIImage(named:"fav.png"), backgroundColor: UIColor.blueColor())]
cell.leftSwipeSettings.transition = MGSwipeTransition.Rotate3D
//configure right buttons
cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor())
,MGSwipeButton(title: "More",backgroundColor: UIColor.lightGrayColor())]
cell.rightSwipeSettings.transition = MGSwipeTransition.Rotate3D
return cell
}
这是我当前项目中的 cellForRowAtIndexPath 函数:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {
cell.request?.cancel()
var img: UIImage?
if let url = post.imageUrl {
img = FeedVCViewController.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, img: img)
return cell
} else {
return PostCell()
}
}
我还需要实现这个回调:
MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: {
(sender: MGSwipeTableCell!) -> Bool in
println("Convenience callback for swipe buttons!")
return true
})
谁能指导我正确的操作方法?由于我是移动开发的新手,所以我只是想让自己省去很多麻烦。
我最困惑的是实现代码中的 "cell" 声明和 "if" 语句以及如何将其与我创建的自定义单元格相结合。
提前致谢!
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell")
等同于
if tableView.dequeueReusableCellWithIdentifier("PostCell") != nil {
let cell = tableView.dequeueReusableCellWithIdentifier("PostCell")
}
他们通过声明一个变量将其设置为可选来做类似的事情。他们检查可选项是否为零。如果是这样,他们会初始化一个新对象并将 cell 设置为等于该对象,这样他们就可以安全地展开。
如果 PostCell 是 MGSwipeTableCell 的子类,您可以像添加 leftButtons 和 rightButtons 一样添加它们。例如
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {
cell.request?.cancel()
var img: UIImage?
if let url = post.imageUrl {
img = FeedVCViewController.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, img: img)
cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: {
(sender: MGSwipeTableCell!) -> Bool in
print("Convenience callback for delete button!")
return true
})
,MGSwipeButton(title: "More",backgroundColor: UIColor.lightGrayColor(),callback: {
(sender: MGSwipeTableCell!) -> Bool in
print("Convenience callback for more button!")
return true
})]
return cell
} else {
return PostCell()
}
}
我正在尝试在我的 Swift 2、Xcode 7 项目中实现可滑动的 table 视图。
我想使用这个库:
https://github.com/MortimerGoro/MGSwipeTableCell
我只是想找出将其实施到我的项目中的正确方法。
我有一个带有 TableView 的 UIView 以及一个自定义 TableViewCell
这里是每个 GitHub repo 的实现代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let reuseIdentifier = "programmaticCell"
var cell = self.table.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
if cell == nil
{
cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
}
cell.textLabel!.text = "Title"
cell.detailTextLabel!.text = "Detail text"
cell.delegate = self //optional
//configure left buttons
cell.leftButtons = [MGSwipeButton(title: "", icon: UIImage(named:"check.png"), backgroundColor: UIColor.greenColor())
,MGSwipeButton(title: "", icon: UIImage(named:"fav.png"), backgroundColor: UIColor.blueColor())]
cell.leftSwipeSettings.transition = MGSwipeTransition.Rotate3D
//configure right buttons
cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor())
,MGSwipeButton(title: "More",backgroundColor: UIColor.lightGrayColor())]
cell.rightSwipeSettings.transition = MGSwipeTransition.Rotate3D
return cell
}
这是我当前项目中的 cellForRowAtIndexPath 函数:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {
cell.request?.cancel()
var img: UIImage?
if let url = post.imageUrl {
img = FeedVCViewController.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, img: img)
return cell
} else {
return PostCell()
}
}
我还需要实现这个回调:
MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: {
(sender: MGSwipeTableCell!) -> Bool in
println("Convenience callback for swipe buttons!")
return true
})
谁能指导我正确的操作方法?由于我是移动开发的新手,所以我只是想让自己省去很多麻烦。
我最困惑的是实现代码中的 "cell" 声明和 "if" 语句以及如何将其与我创建的自定义单元格相结合。
提前致谢!
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell")
等同于
if tableView.dequeueReusableCellWithIdentifier("PostCell") != nil {
let cell = tableView.dequeueReusableCellWithIdentifier("PostCell")
}
他们通过声明一个变量将其设置为可选来做类似的事情。他们检查可选项是否为零。如果是这样,他们会初始化一个新对象并将 cell 设置为等于该对象,这样他们就可以安全地展开。
如果 PostCell 是 MGSwipeTableCell 的子类,您可以像添加 leftButtons 和 rightButtons 一样添加它们。例如
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {
cell.request?.cancel()
var img: UIImage?
if let url = post.imageUrl {
img = FeedVCViewController.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, img: img)
cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: {
(sender: MGSwipeTableCell!) -> Bool in
print("Convenience callback for delete button!")
return true
})
,MGSwipeButton(title: "More",backgroundColor: UIColor.lightGrayColor(),callback: {
(sender: MGSwipeTableCell!) -> Bool in
print("Convenience callback for more button!")
return true
})]
return cell
} else {
return PostCell()
}
}