在平常我们做App的时候,很多时候都需要使用一个动态的个人信息列表, 下面让我们来看看。
PS: 已经更新到Swift 2.1, 支持 iOS 9.1
1.搭建界面
更详细的布局请看Demo, 链接在文章最尾端
2.实现代码
class PersonInfoTableViewCell: UITableViewCell {
@IBOutlet weak var headImage: UIImageView!
@IBOutlet weak var userName: UILabel!
@IBOutlet weak var tipsLabel: UILabel!
@IBOutlet weak var tipsContent: UILabel!
@IBOutlet weak var infoButton: UIButton!
@IBOutlet weak var arrowButton: UIButton!
@IBOutlet weak var delegateImage: UIImageView!
@IBOutlet weak var bandName: UILabel!
var bandWeb = ""var phoneNumber = ""
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
// 0: 拨打自己邀请好友的电话, 1: 拨打上级的电话
@IBAction func callPhone(sender: UIButton) {
if sender.tag == 0 {
print("拨打自己邀请好友的电话 = \(phoneNumber)")
} else if sender.tag == 1 {
print("拨打上级的电话 = \(phoneNumber)")
}
}
// 0: 所属团队, 1: 查看我的邀请, 2: 查看官方授权证书
@IBAction func arrowButton(sender: UIButton) {
if sender.tag == 0 {
print("所属团队")
} else if sender.tag == 1 {
print("查看我的邀请")
} else if sender.tag == 2 {
print("查看官方授权证书")
}
}
}
我们再来看看PersonInfoTableView这个类的代码
class PersonInfoTableView: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var personInfoTableView: UITableView!
var goupState = 2 // 0: 未加入, 1: 正在审核, 2:已经加入, 3:退出审核
var role = 2 // 0: 创始人, 1: 团长, 2: 米客
var isSelf = 0 // 0: 查看自己, 1: 查看别人
var userName = "沙滩裤哥"
var phoneNumber = "321" // 非自己邀请为空, 自己邀请非空
var storeName = "沙滩裤"
var storeInfo = "沙滩裤专卖店"
var teamName = "热米官方店"
var teamCaptain = "热米热米"
var myInvite = "共邀请了12个好友"
var captainPhone = "41312"
var data = [
(bandName: "[若泉]金牌代理", isOfficial: true, bandWeb: "http://www.baidu.com"),
(bandName: "[老干爹]三级代理", isOfficial: true, bandWeb: "http://www.baidu.com"),
(bandName: "[老干妈]二级代理", isOfficial: false, bandWeb: "http://www.baidu.com"),
(bandName: "[美特斯邦威]一级代理", isOfficial: false, bandWeb: "http://www.baidu.com")
]
override func viewDidLoad() {
super.viewDidLoad()
personInfoTableView.estimatedRowHeight = 192
personInfoTableView.rowHeight = UITableViewAutomaticDimension
if isSelf == 0 {
let rightBarButtonItem = UIBarButtonItem(title: "修改", style: UIBarButtonItemStyle.Plain, target: self, action: "rightBarButtonAction")
self.navigationItem.rightBarButtonItem = rightBarButtonItem
}
}
func rightBarButtonAction() {
print("跳转到修改资料的Controller")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if goupState != 2 {
if isSelf == 0 && role == 2 && goupState != 2 {
return 4
}
} else {
if isSelf == 0 && role == 0 || isSelf == 1 && role == 0 || isSelf == 1 && role == 1 {
return 6 + data.count
} else if isSelf == 0 && role == 1 || isSelf == 0 && role == 2 {
return 7 + data.count
} else if isSelf == 1 && role == 2 && phoneNumber != "" {
return 4
} else if isSelf == 1 && role == 2 && phoneNumber == "" {
return 3
}
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: PersonInfoTableViewCell!
if goupState != 2 {
switch indexPath.row {
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "手机"
cell.tipsContent.text = phoneNumber
cell.tipsContent.font = UIFont(name: "System", size: 14.0)
cell.phoneNumber = phoneNumber
cell.arrowButton.hidden = true
break
case 2:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺名称"
cell.tipsContent.text = storeName
cell.arrowButton.hidden = true
break
case 3:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺介绍"
cell.tipsContent.text = storeInfo
cell.arrowButton.hidden = true
cell.infoButton.hidden = true
break
default:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellOne", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.headImage.image = UIImage(named: "image_headDefault")
cell.userName.text = userName
break
}
} else {
if isSelf == 0 && role == 0 || isSelf == 1 && role == 0 || isSelf == 1 && role == 1 { // 创始人查看自己
switch indexPath.row {
case 0:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellOne", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.headImage.image = UIImage(named: "image_headDefault")
cell.userName.text = userName
break
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "手机"
cell.tipsContent.text = phoneNumber
cell.tipsContent.font = UIFont(name: "System", size: 14.0)
cell.arrowButton.hidden = true
cell.phoneNumber = phoneNumber
break
case 2:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺名称"
cell.tipsContent.text = storeName
cell.arrowButton.hidden = true
break
case 3:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺介绍"
cell.tipsContent.text = storeInfo
cell.arrowButton.hidden = true
cell.infoButton.hidden = true
break
case 4:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "所属团队"
cell.tipsContent.text = teamName
cell.delegateImage.hidden = true
break
case 5:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "我的邀请"
cell.tipsContent.text = myInvite
cell.infoButton.hidden = true
cell.arrowButton.tag = 1
break
case 6:
let item = data[indexPath.row - 6]
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
if item.isOfficial {
cell.delegateImage.hidden = false
cell.arrowButton.tag = 2
}
cell.bandName.hidden = false
cell.bandName.text = item.bandName
cell.tipsContent.hidden = true
cell.tipsLabel.hidden = true
break
default:
let item = data[indexPath.row - 6]
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
if item.isOfficial {
cell.delegateImage.hidden = false
cell.arrowButton.tag = 2
} else {
cell.arrowButton.hidden = true
}
cell.bandName.hidden = false
cell.bandName.text = item.bandName
cell.tipsContent.hidden = true
cell.tipsLabel.hidden = true
cell.infoButton.hidden = true
break
}
} else if isSelf == 0 && role == 1 || isSelf == 0 && role == 2 { // 团长查看自己, 米客查看自己
switch indexPath.row {
case 0:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellOne", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.headImage.image = UIImage(named: "image_headDefault")
cell.userName.text = userName
break
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "手机"
cell.tipsContent.text = phoneNumber
cell.tipsContent.font = UIFont(name: "System", size: 14.0)
cell.arrowButton.hidden = true
cell.phoneNumber = phoneNumber
break
case 2:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺名称"
cell.tipsContent.text = storeName
cell.arrowButton.hidden = true
break
case 3:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺介绍"
cell.tipsContent.text = storeInfo
cell.arrowButton.hidden = true
cell.infoButton.hidden = true
break
case 4:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "所属团队"
cell.tipsContent.text = teamName
cell.delegateImage.hidden = true
break
case 5:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "直属上级"
cell.tipsContent.text = teamCaptain
cell.phoneNumber = captainPhone
cell.infoButton.tag = 1
cell.infoButton.setTitle("联系", forState: UIControlState())
cell.arrowButton.hidden = true
break
case 6:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "我的邀请"
cell.tipsContent.text = myInvite
cell.infoButton.hidden = true
cell.arrowButton.tag = 1
break
case 7:
let item = data[indexPath.row - 7]
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
if item.isOfficial {
cell.delegateImage.hidden = false
cell.arrowButton.tag = 2
}
cell.bandName.hidden = false
cell.bandName.text = item.bandName
cell.tipsContent.hidden = true
cell.tipsLabel.hidden = true
break
default:
let item = data[indexPath.row - 7]
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
if item.isOfficial {
cell.delegateImage.hidden = false
cell.arrowButton.tag = 2
} else {
cell.arrowButton.hidden = true
}
cell.bandName.hidden = false
cell.bandName.text = item.bandName
cell.tipsContent.hidden = true
cell.tipsLabel.hidden = true
cell.infoButton.hidden = true
break
}
} else if isSelf == 1 && role == 2 && phoneNumber != "" {
switch indexPath.row {
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "手机"
cell.tipsContent.text = phoneNumber
cell.tipsContent.font = UIFont(name: "System", size: 14.0)
cell.arrowButton.hidden = true
cell.phoneNumber = phoneNumber
break
case 2:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺名称"
cell.tipsContent.text = storeName
cell.arrowButton.hidden = true
break
case 3:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺介绍"
cell.tipsContent.text = storeInfo
cell.arrowButton.hidden = true
cell.infoButton.hidden = true
break
default:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellOne", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.headImage.image = UIImage(named: "image_headDefault")
cell.userName.text = userName
break
}
} else if isSelf == 1 && role == 2 && phoneNumber == "" {
switch indexPath.row {
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellThree", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺名称"
cell.tipsContent.text = storeName
cell.arrowButton.hidden = true
break
case 2:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellTwo", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.tipsLabel.text = "店铺介绍"
cell.tipsContent.text = storeInfo
cell.arrowButton.hidden = true
cell.infoButton.hidden = true
break
default:
cell = tableView.dequeueReusableCellWithIdentifier("tableCellOne", forIndexPath: indexPath) as! PersonInfoTableViewCell
cell.headImage.image = UIImage(named: "image_headDefault")
cell.userName.text = userName
break
}
}
}
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if goupState != 2 {
if isSelf == 0 && role == 2 {
if indexPath.row == 0 {
return 78
} else if indexPath.row == 1 {
return 54
} else if indexPath.row == 2 {
return 65
}
}
} else {
if isSelf == 0 && role == 0 || isSelf == 1 && role == 0 || isSelf == 1 && role == 1 {
if indexPath.row == 0 {
return 78
} else if indexPath.row == 1 || indexPath.row == 5 {
return 54
} else if indexPath.row == 2 || indexPath.row == 4 || indexPath.row == 6 {
return 65
}
} else if isSelf == 0 && role == 1 || isSelf == 0 && role == 2 {
if indexPath.row == 0 {
return 78
} else if indexPath.row == 1 || indexPath.row == 5 || indexPath.row == 6 {
return 54
} else if indexPath.row == 2 || indexPath.row == 4 || indexPath.row == 7 {
return 65
}
} else if isSelf == 1 && role == 2 && phoneNumber != "" {
if indexPath.row == 0 {
return 78
} else if indexPath.row == 1 {
return 54
} else if indexPath.row == 2 {
return 65
}
} else if isSelf == 1 && role == 2 && phoneNumber == "" {
if indexPath.row == 0 {
return 78
} else if indexPath.row == 1 {
return 65
}
}
}
return UITableViewAutomaticDimension
}
}
3.最终效果
项目地址: 链接: http://pan.baidu.com/s/1jGD7AR0 密码: d48u