IOS SWIFT UITableView 实现简单微博列表
//
// 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
}
}
IOS SWIFT UITableView 实现简单微博列表的更多相关文章
- iOS 新浪微博-5.0 首页微博列表
首页显示微博列表,是微博的核心部分,这一章节,我们主要是显示出微博的列表. 导入第三方类库 pod 'SDWebImage', '~> 3.7.3' pod 'MJRefresh', '~> ...
- iOS UI基础-9.2 UITableView 简单微博列表
概述 我们要实现的效果: 这个界面布局也是UITableView实现的,其中的内容就是UITableViewCell,只是这个UITableViewCell是用户自定义实现的.虽然系统自带的UITab ...
- iOS 新浪微博-5.3 首页微博列表_集成图片浏览器
实际上,我们可以使用李明杰在教程里集成的MJPhotoBrowser,地址: http://code4app.com/ios/快速集成图片浏览器/525e06116803fa7b0a000001 使用 ...
- iOS 新浪微博-5.2 首页微博列表_转发微博/工具栏
继续于上一篇,还是做首页的功能,这一篇把剩下的首页继续完善. 看看上面的图片,分析: 1.转发微博里面的内容,和原创微博是一样的,由文字+配图组成.这应该放在一个UIView里处理. 2.工具栏也当成 ...
- ios初识UITableView及简单用法二(模型数据)
// // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...
- iOS 新浪微博-5.1 首页微博列表_时间/配图
在上一篇中,我们已经把首页微博显示出来了,但还有很多细节,需要我们去调整的.这一章中,我们将处理好时间,配图,工具框及转发微博等小细节的功能. 时间处理 第一步:定义一个时间的类别,用于判断是昨天.今 ...
- ios初识UITableView及简单用法一
// // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...
- swift写一个简单的列表unable to dequeue a cell with identifier reuseIdentifier - must register a nib or a cla
报错:unable to dequeue a cell with identifier reuseIdentifier - must register a nib or a class for the ...
- 李洪强iOS开发Swift篇—01_简单介绍
李洪强iOS开发Swift篇—01_简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objec ...
随机推荐
- hdu4300之KMP&&EKMP
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- SQL执行效率总结
1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况 p4: 2.4 内存: 1 G os: windows 2003 数据库: ms sql server 2000 目的: 查询 ...
- JavaScript中的事件处理程序
JavaScript和HTML之间的交互是通过事件实现的.事件,就是文档或者浏览器窗口中发生的一些特定的交互瞬间.可以使用事件处理程序来预订事件,以便在事件发生的时候执行响应的代码.这种观察者模式的模 ...
- [译]Stairway to Integration Services Level 16 – Flexible Source Locations (多文件导入)
介绍 在本文中我们将利用SSIS参数,变量 以及 Foreach Loop Container 从多个源动态导入数据. 开始前我们先下载一些数据.WeatherData_Dec08_Apr09.zip ...
- BZOJ 3175: [Tjoi2013]攻击装置( 匈牙利 )
黑白染成二分图, 然后不能同时选的就连边, 最大匹配数为m, t为不能放的数目, 则题目所求最大点独立集为 n*n-m-t -------------------------------------- ...
- COM编程-注册DLL形式的COM服务器
这篇文章不涉及任何的有关COM的技术的讲解,仅仅的说一下写好的DLL形式的COM怎么使用.如下图所示,我已经有了一个DLL形式的COM服务器和一个使用COM服务器的COM客户端: 现在这个DLL的CO ...
- Nginx 之四: Nginx服务器的rewrite、全局变量、重定向和防盗链相关功能
一:Nginx 后端服务器组的配置: 1.upstream: 用于设置后端服务器组的主要指令,upstream类似于之前的server块或http块,用法如下: upstreame Myserver{ ...
- 我的ubuntu
题外话:不知不觉也已经大三,最近思考了很多.在腾讯网看到了对李嘉诚的一篇专访,感触颇深. 想起来我从第一次接触ubuntu到现在也有一年了,记得第一个版本还是12.04,不过很快就换成了12.10,在 ...
- Error Unable to start the Genymotion virtual device.解决
The Genymotion virtual device could not obtain an IP address.For an unknown reason.VirtualBox DHCP h ...
- UVA 10057 A mid-summer night's dream. 仲夏夜之梦 求中位数
题意:求中位数,以及能成为中位数的数的个数,以及选择不同中位数中间的可能性. 也就是说当数组个数为奇数时,中位数就只有一个,中间那个以及中位数相等的数都能成为中位数,选择的中位数就只有一种可能:如果为 ...