//
// 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. mahout贝叶斯算法开发思路(拓展篇)1

    首先说明一点,此篇blog解决的问题是就下面的数据如何应用mahout中的贝叶斯算法?(这个问题是在上篇(...完结篇)blog最后留的问题,如果想直接使用该工具,可以在mahout贝叶斯算法拓展下载 ...

  2. QQ聊天原理初识

    1:qq之间文件的传输是通过p2p通信进行的. 2:qq之间的表情发送实际上就是文字的发送,是client再接受到文字之后在本地自己进行转换 3:qq之间的通信既能够通过udp也能够通过Tcp 尽管u ...

  3. MessageDigest简单介绍

    本文博客原文 參考文章:http://blog.sina.com.cn/s/blog_4f36423201000c1e.html 一.概述 java.security.MessageDigest类用于 ...

  4. c语言: 文件io, 拷贝文件(二进制)

    #include <stdio.h> #include <stdlib.h> #define TRAN_SZIE 1024 int copy_bin(char* from, c ...

  5. [C#参考]事件和委托的关系

    前面说了委托,接下来就要说一说事件了,同时最后再说一下委托和事件的区别. 事件和委托很相似,事件就好像是被简化的针对特殊用途的委托.看下面的图: 从这张图中能看到,事件是发布者的一个成员,它不是类型. ...

  6. Java安全机制之泛型(JDK1.5)

    泛型,类型安全机制. 好处: 1.将运行时期出现问题ClassCastException转移到了编译时期,方便解决问题,减少运行时期的问题,有利于程序的健壮性. 2.避免了强制转换的麻烦 泛型格式: ...

  7. easyui好例子,值得借鉴

    http://www.cnblogs.com/wuhuacong/p/3317223.html

  8. Yii2 国际化的问题 zh-CN

    代码增加位置: 在项目文件目录的config->main.php 的 return 内增加以下内容,并在项目目录中新建messages 内设置 en-US 和zh-CN 文件夹.zh-CN文件夹 ...

  9. (Problem 22)Names scores

    Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-tho ...

  10. 浏览器对body节点scrollTop解析的差异

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style t ...