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 ...
随机推荐
- GridView事件分析
GridView事件分析 (转) P1默认数据绑定过程 编号 事件名称 作用 E1 DataBinding 数据绑定之前触发,在这个事件之前(第一次生成GridView),GridView不存在行数据 ...
- js动画学习(二)
四.简单动画之缓冲运动 实现速度的缓冲,即不同位置的速度不同,越靠近目标值速度越小,所以速度值与目标值与当前值之差成正比.这里要注意一个问题就是物体在运动中速度是连续变化的,不是按照整数变化的,当物体 ...
- linux shell: 搜索字符串,剔除包含特定字符的行
搜索代码中出现CONFIG_BOOTARGS的行,但又不希望有#define和#undef, 暂时不知带如何直接用一个语句做到,因此用了如下三个语句. find . -type f | xargs g ...
- BZOJ 3293 分金币
整体来说,这道题与之前做的1045题目完全一样,出了说法不一样外,思路及做法可以照搬,因此在这里便不再详解. 程序如下:(如有疑问请参看我的博客http://www.cnblogs.co ...
- Onvif协议
ONVIF致力于通过全球性的开放界面标准来推进网络视频在安防市场的应用,这一接口界面标准将确保不同厂商生产的网络视频监控产品具有互通性.2008年11月,论坛正式发布了ONVIF第一版规范ONVIF核 ...
- Python网络编程——设定并获取默认的套接字超时时间
Sometimes,you need to manipulate the default values of certain properties of a socket library, for e ...
- (zz)Linux下Gcc生成和使用静态库和动态库详解
http://blog.chinaunix.net/uid-23592843-id-223539.html
- ipa制作
打包ipa步骤: 项目名称 -> edit scheme -> 如图选择release 点击close后,选择真机 然后command+b编译程序,右击app,show in Finder ...
- iOS SDK:预览和打开文档
iOS中的沙盒可以让平台更加的安全,这也是沙盒给用户带来的最主要好处.不过由于沙盒的严格限制,导致程序之间共享数据比较麻烦.一般在程序间共享文档可以通过UIDocumentInteractionCon ...
- Uva 3226 Symmetry
题目给出一些点的坐标(横坐标,纵坐标),没有重叠的点,求是否存在一条竖线(平行于y轴的线),使线两边的点左右对称. 我的思路:对于相同的纵坐标的点,即y值相同的点,可以将x的总和计算出,然后除以点的数 ...