命名为NewsCell
于是我们得到了两个文件NewsCell.swift和NewsCell.xib
下面我们开始布局NewsCell.xib,我们要实现这种效果
tableviewcell里面要放置一个UILabel和一个UIImageView
于是我们在自定义tableviewcell里面布局如下
我们在NewsCell.swift和NewsCell.xib两个文件里面操作
我们得到的NewsCell.swift的文件如下
import Foundation
import UIKit
class 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
}
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")
}
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)
}