swift学习六天 项目实战-知乎日报之UITabelView使用
来源:互联网 发布:网络捐款平台有哪些 编辑:程序博客网 时间:2024/05/18 17:04
这里附上代码下载地址:
http://download.csdn.net/detail/guchengyunfeng/7989139
上篇写了接口部分,现在来开始开发界面部分.
界面开发的大致思路跟使用OC的时候差不多,感觉就是将OC的语法用swift翻译 一遍,有什么API不会的可以直接进去查看属性,方法和委托。
先看界面的效果图
首先开始自定义一个cell
选中File->New File
选中Source->swift File
将名称命名为NewsCell,这样就新建了一个NewsCell.swift文件
然后再选中File->New File
选择User Interface->View
设备选择iphone
命名为NewsCell
于是我们得到了两个文件NewsCell.swift和NewsCell.xib
下面我们开始布局NewsCell.xib,我们要实现这种效果
tableviewcell里面要放置一个UILabel和一个UIImageView
于是我们在自定义tableviewcell里面布局如下
我们在NewsCell.swift和NewsCell.xib两个文件里面操作
我们得到的NewsCell.swift的文件如下
import Foundationimport UIKitclass NewsCell:UITableViewCell{ @IBOutlet var NImg : UIImageView @IBOutlet var NLabContent : UILabel}
现在完成了自定义的TabelViewCell
下面演示如何在代码中使用NewsCell
func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { var cell = tableView?.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as? NewsCell var index = indexPath!.row var data = self.jsonArrStories[index] as NSDictionary var sTitle=data.objectForKey("title") as String cell!.NLabContent.text=sTitle var arrImgURL=data.objectForKey("images") as NSArray var imagURL=arrImgURL[0] as String println("imageURL:\(imagURL)") cell!.NImg.setImage(imagURL,placeHolder: UIImage(named: "001p9BkFgy6KqjPYZg74b&690.jpeg")) return cell }
我们对比之前用OC开发的时候的代码(随便拷贝一份过来的)
static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; static BOOL nibsRegistered = NO; if (!nibsRegistered) { UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier]; nibsRegistered = YES; } CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; NSUInteger row = [indexPath row]; NSDictionary *rowData = [self.dataList objectAtIndex:row]; cell.name = [rowData objectForKey:@"name"]; cell.dec = [rowData objectForKey:@"dec"]; cell.loc = [rowData objectForKey:@"loc"]; cell.image = [imageList objectAtIndex:row]; return cell;
大致思路是差不多,只不过语法方面有些差异,直接翻译过来就好了
下面我们初始化TabelView
func setupViews() { //tableView tabNewList.delegate=self tabNewList.dataSource=self var nib = UINib(nibName:"NewsCell", bundle: nil) self.tabNewList?.registerNib(nib, forCellReuseIdentifier: identifier) //scrollView scrollNewList.delegate=self var pageCount=jsonArrtop_stories.count println("pageCount\(pageCount)") scrollNewList.contentSize=CGSizeMake(CGFloat(pageCount*320), CGFloat(140)) scrollNewList.pagingEnabled=true self.addImgsToScroll(scrollNewList, arrStory_Top: self.jsonArrtop_stories) //pageConrol pageNewList.numberOfPages=pageCount pageNewList.currentPage=1 var dic1=jsonArrtop_stories.objectAtIndex(0) as NSDictionary var title1=dic1["title"] as String labScrollTitle.text=title1 //RefreshControl self.addRefreshView("PullUpView") // self.addRefreshView("PullDownView") }
然后实现几个tableview的接口
func numberOfSectionsInTableView(tableView: UITableView?) -> Int { // #warning Potentially incomplete method implementation. // Return the number of sections. return 1 } func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat { return 104 } func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete method implementation. // Return the number of rows in the section. return jsonArrStories.count } func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { var cell = tableView?.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as? NewsCell var index = indexPath!.row var data = self.jsonArrStories[index] as NSDictionary var sTitle=data.objectForKey("title") as String cell!.NLabContent.text=sTitle var arrImgURL=data.objectForKey("images") as NSArray var imagURL=arrImgURL[0] as String println("imageURL:\(imagURL)") cell!.NImg.setImage(imagURL,placeHolder: UIImage(named: "001p9BkFgy6KqjPYZg74b&690.jpeg")) return cell } func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { var index = indexPath!.row var data = self.jsonArrStories[index] as NSDictionary var aId : Int=data.objectForKey("id") as Int var board:UIStoryboard = UIStoryboard(name:"Main", bundle:nil); var detailConrol=board.instantiateViewControllerWithIdentifier("KDNewsDetailController") as KDNewsDetailController detailConrol.aId=aId println("detailConrol.id\(aId)") // self.showViewController(detailConrol, sender: self) self.presentModalViewController(detailConrol, animated: true) }
ok,到这里就完成了这种效果,下一篇介绍使用UIScollView和UIPageConrol以及定时器实现上面滚动的效果
这里附上代码下载地址:
http://download.csdn.net/detail/guchengyunfeng/7989139
0 0
- swift学习六天 项目实战-知乎日报之UITabelView使用
- swift学习第八天 项目实战-知乎日报之UIScollView和UIpageConrolView使用
- swift学习第八天 项目实战-知乎日报之UIWebView使用
- swift学习五天 项目实战-知乎日报之网络交互NSURLConnection
- 【项目源码】- 【模仿知乎日报】模仿知乎日报
- 开源项目学习笔记-知乎日报purify
- 学习知乎日报--day2
- 学习知乎日报---day3
- 学习知乎日报---day4
- 学习知乎日报-----day5
- 【项目源码】- 【模仿知乎日报二】吐血高仿知乎日报
- 学习知乎日报客户端--Day1
- 使用 vue2 制作一个知乎日报
- 使用 vue2 制作一个知乎日报
- RxAndroid项目实践-使用RxJava响应式编码实现知乎日报的欢迎界面
- 知乎日报客户端
- 知乎日报API
- 知乎日报API
- 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
- ps简单实用
- 关于各种项目的SVN 版本控制忽略文件类型搜集
- JQuery Easy Ui dataGrid 数据表格
- 选择性阅读
- swift学习六天 项目实战-知乎日报之UITabelView使用
- Java的IO结构图和集合结构图
- STL (map , queue , priority_queue , vector , list)的简介
- hdu 1010 Tempter of the Bone(dfs剪枝+回溯)
- javaweb项目和java项目之间的转换
- C++中error C2440: “=”: 无法从“void *”转换为“graph_node **”
- 椭圆的第一要素
- Linux 服务器日志文件查找技巧精粹(1)
- 『ANDROID』FragmentTabHost的应用(fragment学习系列稿件之二)