//
// 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 实现简单微博列表的更多相关文章

  1. iOS 新浪微博-5.0 首页微博列表

    首页显示微博列表,是微博的核心部分,这一章节,我们主要是显示出微博的列表. 导入第三方类库 pod 'SDWebImage', '~> 3.7.3' pod 'MJRefresh', '~> ...

  2. iOS UI基础-9.2 UITableView 简单微博列表

    概述 我们要实现的效果: 这个界面布局也是UITableView实现的,其中的内容就是UITableViewCell,只是这个UITableViewCell是用户自定义实现的.虽然系统自带的UITab ...

  3. iOS 新浪微博-5.3 首页微博列表_集成图片浏览器

    实际上,我们可以使用李明杰在教程里集成的MJPhotoBrowser,地址: http://code4app.com/ios/快速集成图片浏览器/525e06116803fa7b0a000001 使用 ...

  4. iOS 新浪微博-5.2 首页微博列表_转发微博/工具栏

    继续于上一篇,还是做首页的功能,这一篇把剩下的首页继续完善. 看看上面的图片,分析: 1.转发微博里面的内容,和原创微博是一样的,由文字+配图组成.这应该放在一个UIView里处理. 2.工具栏也当成 ...

  5. ios初识UITableView及简单用法二(模型数据)

    // // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...

  6. iOS 新浪微博-5.1 首页微博列表_时间/配图

    在上一篇中,我们已经把首页微博显示出来了,但还有很多细节,需要我们去调整的.这一章中,我们将处理好时间,配图,工具框及转发微博等小细节的功能. 时间处理 第一步:定义一个时间的类别,用于判断是昨天.今 ...

  7. ios初识UITableView及简单用法一

    // // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...

  8. 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 ...

  9. 李洪强iOS开发Swift篇—01_简单介绍

    李洪强iOS开发Swift篇—01_简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objec ...

随机推荐

  1. word中创建文本框

    word中创建文本框         在插入中点击"文本框"选项卡,例如以下图所看到的:        手工加入自己想要的文本框格式,然后选择所创建的文本框,在工具栏处会发现多了一 ...

  2. DMA(STM32)

     1.DMA:data memory access //实际的内存存储 注:DMA干活的时候是不须要CPU干涉的 2. ①内存(定义的变量)---外设(寄存器). ②内存---内存 ③外设---外 ...

  3. Oracle 表三种连接方式(sql优化)

    在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式: 嵌套循环(Nested Loops (NL)) (散列)哈希 ...

  4. VS2008编译iconv静态链接库

    iconv是将一种编码格式转换为还有一种编码格式的开源库,比如能够把Windows环境下通用的ASCii(中文是GB2312)编码转换为国际通用的Unicode编码 iconv最新版本号仅仅支持Min ...

  5. 同一个shell下实现多个composite的切换

    SWT实现面板切换可以采用StackLayout来实现,通过topControl来实现置顶,但是置顶之后要记得调用父组件的layout方法.否则会不显示,应该所有改变组件布局的情况都要进行父组件的重新 ...

  6. [Asp.net]常见word,excel,ppt,pdf在线预览方案(转)

    引言 之前项目需要,查找了office文档在线预览的解决方案,顺便记录一下,方便以后查询. 方案一 直接在浏览器中打开Office文档在页面上的链接.会弹出如下窗口: 优点:主流浏览器都支持. 缺点: ...

  7. No.2小白的HTML+CSS心得篇

    今天要强调的重点是分析把握好HTML标签的两个方面: 1.标签的用途(用途指的是用来干什么,有什么作用)在专业术语叫作 语义化. 举个列子:<br/> 换行的作用 见了它就明白它的语义就是 ...

  8. xml校验问题

    struts2使用xml校验 按照书本输入dtd约束文件 "-//OpenSymphony Group//XWork Validator 1.0.2//EN""http: ...

  9. openrisc 之 Wishbone总线学习笔记——接口信号定义

    这部分内容就是copy下来的,网上到处都有.先看看接口啥样子,在详细说明 接口定义copy http://blog.csdn.net/ce123/article/details/6929897.百度文 ...

  10. hibernate 持久化对象的生命周期 2.1

    持久化对象的生命周期 瞬态(自由态) 表示对象在内存中存在,在数据库中没有数据相关,比如刚刚new出来的一个对象 持久态 持久态指的是持久化对象处于由Hibernate管理的状态,这种状态下持久化对象 ...