如何从json调用评分视图并在表格视图中显示评分 [英] how to call rating view from json and show the rating in my table view
问题描述
我有一个自定义单元格,其中有名称",地址",评分视图".评级视图是一个单独的类别库文件,用于评级视图,它将具有3个图像(全星,半星,空星).现在从我的json数据中,我对每个单元格都有一些评估值.像下面的json结构:
I have one custom cell, in that i have " name", "address" "rating view". Rating view is one class separately library file for rating view it will have some 3 images ( full star, half star, empty star ). Now from my json data i have some rating values for each cell. like below json structure :
这是我的自定义单元格:
This is my custom cell :
class customCell: UITableViewCell {
@IBOutlet weak var vendorName: UILabel! // vendor label name
@IBOutlet weak var vendorAddress: UILabel! // vendor address aname
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
我的表格视图控制器:
我还有2个自定义单元格.但是,如果我尝试为一个单元格添加内容,我将编写并理解代码,而我将为所有自定义单元格做事.
i have 2 more custom cell.But if i try to add for one cell i will make and understand code and i will do for all custom cell.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// if cell tap condition
if(isTapped == true && indexPath == selectedIndex)
{
if (premiumUserCheck && indexPath == selectedIndex ) {
let cell1:premiumUsercell = self.tableView.dequeueReusableCellWithIdentifier("cell3") as! premiumUsercell
cell1.phoneNumber = (arrDict[indexPath.section] .valueForKey("phone") as? String)!
cell1.vendorName3.text=arrDict[indexPath.section] .valueForKey("name") as? String
cell1.vendorAdddress3.text=arrDict[indexPath.section] .valueForKey("address") as? String
print("premium user")
return cell1
}
else {
let cell1:ExpandCell = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! ExpandCell
cell1.VendorName.text=arrDict[indexPath.section] .valueForKey("name") as? String
cell1.vendorAdress.text=arrDict[indexPath.section] .valueForKey("address") as? String
//cell1.externalView.hidden = true
print("non premium user")
return cell1
}
}
// show default cutsom cell
let cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell
cell.vendorName.text=arrDict[indexPath.section] .valueForKey("name") as? String
cell.vendorAddress.text=arrDict[indexPath.section] .valueForKey("address") as? String
print("norml user")
return cell
}
在下面的代码中,评级视图是一些库文件,我正在使用这些文件进行评级视图.我将自定义视图放在自定义单元格中的一个视图下.我必须在我的自定义单元格中选择我的视图.而且我必须将该特定类分配为RatingView
类..然后,如果在自定义单元格中单击我的评分视图,我将看到下图所示的图像,以设置评分星级,星级,关闭,空白一半的图片:
Here below code is rating view is some library files , which i am using for rating view. i place custom view under one view inside my custom cell.I have to select my view in my custom cell. And i have to assign that particular class as RatingView
classes..Then if i click my rating view in my custom cell , i can see like below image to set `rating star, number of star, off, empty half image:
我的ratingviewclasses
:
import UIKit
@objc public protocol RatingViewDelegate {
/**
Called when user's touch ends
- parameter ratingView: Rating view, which calls this method
- parameter didChangeRating newRating: New rating
*/
func ratingView(ratingView: RatingView, didChangeRating newRating: Float)
}
/**
Rating bar, fully customisable from Interface builder
*/
@IBDesignable
public class RatingView: UIView {
/// Total number of stars
@IBInspectable public var starCount: Int = 5
/// Image of unlit star, if nil "starryStars_off" is used
@IBInspectable public var offImage: UIImage?
/// Image of fully lit star, if nil "starryStars_on" is used
@IBInspectable public var onImage: UIImage?
/// Image of half-lit star, if nil "starryStars_half" is used
@IBInspectable public var halfImage: UIImage?
/// Current rating, updates star images after setting
@IBInspectable public var rating: Float = Float(0) {
didSet {
// If rating is more than starCount simply set it to starCount
rating = min(Float(starCount), rating)
updateRating()
}
}
/// If set to "false" only full stars will be lit
@IBInspectable public var halfStarsAllowed: Bool = true
/// If set to "false" user will not be able to edit the rating
@IBInspectable public var editable: Bool = true
/// Delegate, must confrom to *RatingViewDelegate* protocol
public weak var delegate: RatingViewDelegate?
var stars = [UIImageView]()
override init(frame: CGRect) {
super.init(frame: frame)
customInit()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override public func awakeFromNib() {
super.awakeFromNib()
customInit()
}
override public func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
customInit()
}
func customInit() {
let bundle = NSBundle(forClass: RatingView.self)
if offImage == nil {
offImage = UIImage(named: "star_empty", inBundle: bundle, compatibleWithTraitCollection: self.traitCollection)
}
if onImage == nil {
onImage = UIImage(named: "star_full", inBundle: bundle, compatibleWithTraitCollection: self.traitCollection)
}
if halfImage == nil {
halfImage = UIImage(named: "star_half_full", inBundle: bundle, compatibleWithTraitCollection: self.traitCollection)
}
guard let offImage = offImage else {
assert(false, "offImage is not set")
return
}
for var i = 1; i <= starCount; i++ {
let iv = UIImageView(image: offImage)
addSubview(iv)
stars.append(iv)
}
layoutStars()
updateRating()
}
override public func layoutSubviews() {
super.layoutSubviews()
layoutStars()
}
func layoutStars() {
if stars.count != 0,
let offImage = stars.first?.image {
let halfWidth = offImage.size.width/2
let distance = (bounds.size.width - (offImage.size.width * CGFloat(starCount))) / CGFloat(starCount + 1) + halfWidth
var i = 1
for iv in stars {
iv.frame = CGRectMake(0, 0, offImage.size.width, offImage.size.height)
iv.center = CGPointMake(CGFloat(i) * distance + halfWidth * CGFloat(i - 1),
self.frame.size.height/2)
i++
}
}
}
/**
Compute and adjust rating when user touches begin/move/end
*/
func handleTouches(touches: Set<UITouch>) {
let touch = touches.first!
let touchLocation = touch.locationInView(self)
for var i = starCount - 1; i >= 0; i-- {
let imageView = stars[i]
let x = touchLocation.x;
if x >= imageView.center.x {
rating = Float(i) + 1
return
} else if x >= CGRectGetMinX(imageView.frame) && halfStarsAllowed {
rating = Float(i) + 0.5
return
}
}
rating = 0
}
/**
Adjust images on image views to represent new rating
*/
func updateRating() {
// To avoid crash when using IB
if stars.count == 0 {
return
}
// Set every full star
var i = 1
for ; i <= Int(rating); i++ {
let star = stars[i-1]
star.image = onImage
}
if i > starCount {
return
}
// Now add a half star
if rating - Float(i) + 1 >= 0.5 {
let star = stars[i-1]
star.image = halfImage
i++
}
for ; i <= starCount; i++ {
let star = stars[i-1]
star.image = offImage
}
}
}
// MARK: Override UIResponder methods
extension RatingView {
override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard editable else { return }
handleTouches(touches)
}
override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard editable else { return }
handleTouches(touches)
}
override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard editable else { return }
handleTouches(touches)
guard let delegate = delegate else { return }
delegate.ratingView(self, didChangeRating: rating)
}
}
现在,我需要从json中获取评分编号,并且必须在自定义单元格中将其分配给uiview,并且需要在所有表格视图单元格中显示相应的评分.
Now i need to get the rating number from my json and i have to assign to my uiview in my custom cell, and i need to show the respective rating in my all table view cell.
请帮帮我.我正在努力地动态获取json数据?
Please help me out. I am strugling to do with getting json data dynamically??
感谢!
已更新:
customcell.swift
@IBOutlet weak var ratingView: RatingView!
@IBOutlet weak var vendorName: UILabel! // vendor label name
@IBOutlet weak var vendorAddress: UILabel! // vendor address aname
override func awakeFromNib() {
super.awakeFromNib()
super.awakeFromNib()
//ratingView = RatingView(frame:CGRectMake(0, 0, cellWidth, cellHeight))
// Initialization code
}
Viewcontroller.swift
Viewcontroller.swift
let cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell
let ratingString = "\(arrDict[indexPath.section].valueForKey("rating"))"
cell.ratingView?.rating = Float(ratingString)!
cell.vendorName.text=arrDict[indexPath.section] .valueForKey("name") as? String
cell.vendorAddress.text=arrDict[indexPath.section] .valueForKey("address") as? String
推荐答案
在TableViewCell
的awakeNib
方法中将RatingView
添加为子视图,并将其作为全局变量,例如ratingView
.然后
Add RatingView
as a subview in TableViewCell
's awakeNib
method and make it as global variable say ratingView
. then
var ratingView:RatingView? = nil
override func awakeFromNib() {
super.awakeFromNib()
ratingView = RatingView(frame:CGRectMake(0, 0, cellWidth, cellHeight)) // your requiredFrame
}
在cellForRowAtIndexPath
let ratingString = "\(arrDict[indexPath.section].valueForKey("rating"))"
if let ratingValue = Float(ratingString) {
cell1.ratingView?.rating = ratingValue
}
else {
cell1.ratingView?.rating = 0
}
这篇关于如何从json调用评分视图并在表格视图中显示评分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!