在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
//
// Weibo.swift
// UITableViewCellExample
//
// Created by XUYAN on 15/8/15.
// Copyright (c) 2015年 com.world. All rights reserved.
//
import Foundation
class Weibo {
//属性
var id : UInt32
var img : String!
var username : String!
var mbtype : String!
var createdOn : String!
var source : String!
var text : String!
//构造函数
init(id : UInt32,img : String,username : String,mbtype : String,createdOn : String,source : String,text : String){
self.id = id
self.img = img
self.username = username
self.mbtype = mbtype
self.createdOn = createdOn
self.source = source
self.text = text
}
}
//
// CustomCell.swift 自定义单元格
// UITableViewCellExample
//
// Created by XUYAN on 15/8/15.
// Copyright (c) 2015年 com.world. All rights reserved.
//
import UIKit
import Foundation
class CustomCell : UITableViewCell{
//头像宽度,高度
var imgView1_W : CGFloat = 50.0
var imgView1_H : CGFloat = 50.0
var imgView1 : UIImageView! //头像控件
var imgView2 : UIImageView! //会员类型
var labelName : UILabel! //用户名
var labeDate : UILabel! //日期
var labelSource : UILabel! //客户端
var labelContent : UILabel! //内容
var weibo : Weibo? //微博内容
var height : CGFloat = 0.0 //高度
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
initView()
}
func initView() -> Void {
//头像
imgView1 = UIImageView()
self.addSubview(imgView1)
//用户名
labelName = UILabel()
labelName.font = UIFont.systemFontOfSize(13)
self.addSubview(labelName)
//会员类型
imgView2 = UIImageView()
self.addSubview(imgView2)
//日期
labeDate = UILabel()
labeDate.font = UIFont.systemFontOfSize(12)
self.addSubview(labeDate)
//客户端
labelSource = UILabel()
labelSource.font = UIFont.systemFontOfSize(12)
self.addSubview(labelSource)
//内容
labelContent = UILabel()
labelContent.font = UIFont.systemFontOfSize(14)
self.addSubview(labelContent)
}
func setData(weibo : Weibo) -> Void{
//设置头像大小和位置
var imgView1_X : CGFloat = 10.0;
var imgView1_Y : CGFloat = 10.0;
var imgView1Rect : CGRect = CGRectMake(imgView1_X, imgView1_Y, imgView1_W, imgView1_H)
imgView1.image = UIImage(named: weibo.img)
imgView1.frame = imgView1Rect
//设置用户名 LABEL 的大小,位置
var labelName_X = CGRectGetMaxX(imgView1.frame) + 10.0 //控件间距 用户名X点
var labelName_Y = imgView1_Y //用户名Y点
labelName.numberOfLines = 0
labelName.lineBreakMode = NSLineBreakMode.ByCharWrapping
labelName.text = weibo.username
var text1 : NSString = NSString(CString: labelName.text!.cStringUsingEncoding(NSUTF8StringEncoding)!, encoding: NSUTF8StringEncoding)!
var rect1 = text1.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, attributes: [NSFontAttributeName : labelName.font], context: nil)
labelName.frame = CGRectMake(labelName_X, labelName_Y , rect1.width, rect1.height)
//会员头像图标大小和位置
var imgView2_X = CGRectGetMaxX(labelName.frame) + 10.0
var imgView2_Y = imgView1_Y
var imgView2Rect = CGRectMake(imgView2_X, imgView2_Y, 13, 13)
imgView2.image = UIImage(named: "")
imgView2.frame = imgView2Rect
//根据发布日期内容取得文本占用空间大小
var labeDate_X = labelName_X
labeDate.numberOfLines = 0
labeDate.lineBreakMode = NSLineBreakMode.ByCharWrapping
labeDate.text = weibo.createdOn
var text2 : NSString = NSString(CString: labeDate.text!.cStringUsingEncoding(NSUTF8StringEncoding)!, encoding: NSUTF8StringEncoding)!
var rect2 = text2.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, attributes: [NSFontAttributeName : labeDate.font], context: nil)
var labeDate_Y = CGRectGetMaxY(imgView1.frame) - rect2.height
labeDate.frame = CGRectMake(labeDate_X, labeDate_Y , rect2.width, rect2.height)
//设备
var labelSource_X = CGRectGetMaxX(labeDate.frame) + 10
var labelSource_Y = labeDate_Y
labelSource.numberOfLines = 0
labelSource.lineBreakMode = NSLineBreakMode.ByCharWrapping
labelSource.text = weibo.source
var text3 : NSString = NSString(CString: labelSource.text!.cStringUsingEncoding(NSUTF8StringEncoding)!, encoding: NSUTF8StringEncoding)!
var rect3 = text3.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, attributes: [NSFontAttributeName : labelSource.font], context: nil)
labelSource.frame = CGRectMake(labelSource_X, labelSource_Y , rect3.width, rect3.height)
//微博主要内容
var labelContent_X = imgView1_X
var labelContent_Y = CGRectGetMaxY(imgView1.frame) + 5
labelContent.numberOfLines = 0
labelContent.lineBreakMode = NSLineBreakMode.ByCharWrapping
labelContent.text = weibo.text!
var attributes4 = [NSFontAttributeName : labelContent.font]
var options4 = NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading
var text4 : NSString = NSString(CString: labelContent.text!.cStringUsingEncoding(NSUTF8StringEncoding)!, encoding: NSUTF8StringEncoding)!
var rect4 = text4.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max), options: options4, attributes: attributes4, context: nil)
labelContent.frame = CGRectMake(labelContent_X, labelContent_Y , 355, rect4.height) //345下,是为IPHONE 6的
height = CGRectGetMaxY(labelContent.frame) + 10 //行下方留10 个点的距离
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//
// ViewController.swift
// UITableViewCellExample
//
// Created by XUYAN on 15/8/15.
// Copyright (c) 2015年 com.world. All rights reserved.
//
import UIKit
import Foundation
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var tableView : UITableView!
var datas : [Weibo] = []
var cells : [AnyObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
initData()
tableView = UITableView(frame: CGRectMake(5, 64, self.view.frame.size.width - 10, self.view.frame.size.height - 64), style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var wb : Weibo = datas[indexPath.row]
var identity = "cellidentity"
var obj : AnyObject? = tableView.dequeueReusableCellWithIdentifier(identity)
var cell : CustomCell!
if (obj == nil) {
cell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: identity)
cell.setData(wb)
} else {
cell = obj as! CustomCell
}
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var cell : CustomCell = cells[indexPath.row] as! CustomCell
cell.setData(datas[indexPath.row])
return cell.height
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func initData() -> Void {
var wb1 = Weibo(id: 100, img: "3.jpg", username: "Echo", mbtype: "", createdOn: "昨天 22:30", source: "iphone 6 Plus", text: "湖北省,位于中国中部偏南、长江中游,洞庭湖以北,故名湖北,简称“鄂”,省会武汉。湖北介于北纬29°05′至33°20′,东经108°21′至116°07′,东连安徽,东南邻江西、湖南,西连重庆,西北与陕西为邻,北接河南。湖北东、西、北三面环山,中部为“鱼米之乡”的江汉平原。")
var wb2 = Weibo(id: 101, img: "1.jpg", username: "Alice", mbtype: "", createdOn: "昨天 22:35", source: "iphone 6 Plus", text: "各位好友,朋友,因本人操作不慎QQ号码被盗,所发表一些污秽言论及诈骗信息。在此我向各位道歉,请各位不要相信这些污秽言论及诈骗信息。对此给各位带来的负面影响我深表歉意。请各位在使用QQ及其他社交工具时谨防这种现象出现,避免悲剧再次发生。最后祝盗号的骗子们,今天晚上死一户口本...")
var wb3 = Weibo(id: 101, img: "2.jpg", username: "小妖精", mbtype: "", createdOn: "昨天 22:55", source: "iphone 6 Plus", text: "what's mean。")
var wb4 = Weibo(id: 101, img: "3.jpg", username: "Echo", mbtype: "", createdOn: "昨天 23:46", source: "iphone 4", text: "测试微博列表。")
var wb5 = Weibo(id: 101, img: "3.jpg", username: "Echo", mbtype: "", createdOn: "昨天 23:47", source: "iphone 4", text: "我又发表了一条微博。")
var wb6 = Weibo(id: 101, img: "3.jpg", username: "Echo", mbtype: "", createdOn: "昨天 23:48", source: "iphone 4", text: "我又发表了一条微博。")
var wb7 = Weibo(id: 101, img: "3.jpg", username: "Echo", mbtype: "", createdOn: "昨天 23:49", source: "iphone 4", text: "各位好友,朋友,因本人操作不慎QQ号码被盗,所发表一些污秽言论及诈骗信息。在此我向各位道歉,请各位不要相信这些污秽言论及诈骗信息。对此给各位带来的负面影响我深表歉意。请各位在使用QQ及其他社交工具时谨防这种现象出现,避免悲剧再次发生。最后祝盗号的骗子们,今天晚上死一户口本...。")
datas.append(wb1)
datas.append(wb2)
datas.append(wb3)
datas.append(wb4)
datas.append(wb5)
datas.append(wb6)
datas.append(wb7)
var cell1 = CustomCell()
cells.append(cell1)
var cell2 = CustomCell()
cells.append(cell2)
var cell3 = CustomCell()
cells.append(cell3)
var cell4 = CustomCell()
cells.append(cell4)
var cell5 = CustomCell()
cells.append(cell5)
var cell6 = CustomCell()
cells.append(cell6)
var cell7 = CustomCell()
cells.append(cell7)
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
}
|
请发表评论