感谢您阅读我的问题。
我试图通过快速编程使自定义UITableViewCell不使用Buider接口(nib文件)
这是我的自定义UITableViewCell(ZHome_Post_TableViewCell_1)文件:
var titlePost:UILabel!
var btnCategory:UIButton!
var timePost:UILabel!
var thumbPost:UIImageView!
var customView:UIView!
var _data_category_name:NSString!
var _data_time_post:NSString!
var _data_title_post:NSString!
var _data_thumb_post:NSString!
let screenWidth:CGFloat = UIScreen.mainScreen().bounds.width
let screenHeight:CGFloat = UIScreen.mainScreen().bounds.height
let screenBounds: CGRect = UIScreen.mainScreen().bounds
let contentViewWidth:CGFloat = UIScreen.mainScreen().bounds.width - 15
let row_height = _screen_width / 1.52
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
_data_category_name = dataHelper.catName
_data_time_post = dataHelper.timePost
_data_title_post = dataHelper.titlePost
_data_thumb_post = dataHelper.urlThumb
self.accessoryType = UITableViewCellAccessoryType.None
self.selectionStyle = UITableViewCellSelectionStyle.None
self.backgroundColor = UIColor(rgb: 0xf0efef)
self.contentView.backgroundColor = UIColor.clearColor()
if (customView == nil){
customView = UIView(frame: CGRectMake(0, 0, contentViewWidth, row_height))
customView.backgroundColor = UIColor.whiteColor()
customView.layer.borderWidth = 0.6
customView.layer.borderColor = UIColor(rgb: 0xd2d2d2).CGColor
customView.layer.shadowColor = UIColor(rgb: 0xd2d2d2).CGColor
customView.layer.shadowOffset = CGSizeMake(0.0, 1)
customView.layer.shadowOpacity = 0.7
customView.layer.cornerRadius = 1
customView.layer.masksToBounds = true
contentView.addSubview(customView)
}
if (thumbPost == nil){
thumbPost = UIImageView(frame: CGRectMake(0, customView.frame.height / 3.0, customView.frame.width, row_height / 1.45))
customView.addSubview(thumbPost)
}
if(titlePost == nil){
titlePost = UILabel(frame: CGRectMake(7, 7, customView.frame.width - 10, row_height / 5.2))
titlePost.font = UIFont(name: "HelveticaNeue-Medium", size: 14)
titlePost.numberOfLines = 2
titlePost.clipsToBounds = true
titlePost.textColor = UIColor(rgb: 0x494949)
titlePost.preferredMaxLayoutWidth = 60
customView.addSubview(titlePost)
}
if(btnCategory == nil){
btnCategory = SFlatButton(frame: CGRectMake(7, titlePost.frame.height + 5 , stringHeper.getSizeOfLabel(_data_category_name.uppercaseString).width + 10, 15), sfButtonType: SFlatButton.SFlatButtonType.SFBCategoryBlue)
btnCategory.setTitle(_data_category_name.uppercaseString, forState: UIControlState.Normal)
btnCategory.titleLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 09)
customView.addSubview(btnCategory)
}
if(timePost == nil){
timePost = UILabel(frame: CGRectMake(btnCategory.frame.width + 15, titlePost.frame.height + 5, stringHeper.getSizeOfLabel(_data_time_post).width + 15, 15))
timePost.text = _data_time_post
timePost.textColor = UIColor(rgb: 0x9d9d9d)
timePost.font = UIFont(name: "HelveticaNeue-Light", size: 09)
customView.addSubview(timePost)
}
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
我初始化函数awakeFromNib()中的所有内容,每次我滚动UITableview时都调用awakeFromNib,这使应用程序缓慢而缓慢:(
因此,如何只初始化一次控件。
谢谢!
更新:
我的cellForRowAtIndexPath:
var _data_url_thumb : String = _dataArray[indexPath.row].objectForKey("thumbnail") as String;
var _data_file_name : String = _dataArray[indexPath.row].objectForKey("fileName") as String;
var _data_title:String = _dataArray[indexPath.row].objectForKey("title") as String;
var _data_cat_name: NSString = _dataArray[indexPath.row].objectForKey("category_name") as String;
var _data_author: NSString = _dataArray[indexPath.row].objectForKey("display_name") as String;
var _data_published_at: NSString = _dataArray[indexPath.row].objectForKey("published_at") as String;
var _data_avatar: NSString = _dataArray[indexPath.row].objectForKey("avatar") as String;
dataHelper.gsCatName = _data_cat_name
dataHelper.gsTimePost = _data_published_at
dataHelper.gsTitlePost = _data_title
dataHelper.gsUrlThumbPost = _data_url_thumb
let cell = UITableViewCell()
if (tableView == tableViewEvent){
if(_dataEventsArray[0]["status"] as NSInteger == 0){
let _data_event_at: String = _dataEventsArray[indexPath.row].objectForKey("event_at") as String
let _data_event_title: String = _dataEventsArray[indexPath.row].objectForKey("title") as String
let _data_event_post_id: String = _dataEventsArray[indexPath.row].objectForKey("post_id") as String
dataHelper._gsEventTime = _data_event_at
dataHelper._gsEventTitle = _data_event_title
dataHelper.gsIdPost = _data_event_post_id
var cell: ZHome_Event_TableViewCell = tableView.dequeueReusableCellWithIdentifier("home_event_custom_tableviewcell", forIndexPath: indexPath) as ZHome_Event_TableViewCell
cell.awakeFromNib()
return cell
}
}
if (tableView == tableViewPost){
if(indexPath.row == 4){
var cell: ZHome_Post_TableViewCell_2 = tableView.dequeueReusableCellWithIdentifier("home_post_custom_tableviewcell_2", forIndexPath: indexPath) as ZHome_Post_TableViewCell_2
cell.awakeFromNib()
ImageHelper.sharedLoader.imageForUrl(_data_url_thumb, completionHandler:{(image: UIImage?, url: String) in
cell.thumbPost.image = image
})
cell.titlePost.text = _data_title
cell.titlePost.sizeToFit()
return cell
}else{
var cell: ZHome_Post_TableViewCell_1 = tableView.dequeueReusableCellWithIdentifier("home_post_custom_tableviewcell_1", forIndexPath: indexPath) as ZHome_Post_TableViewCell_1
cell.awakeFromNib()
ImageHelper.sharedLoader.imageForUrl(_data_url_thumb, completionHandler:{(image: UIImage?, url: String) in
cell.thumbPost.image = image
})
cell.titlePost.text = _data_title
cell.titlePost.sizeToFit()
return cell
}
}
return cell
最佳答案
您没有重用tableview单元格。这将占用大量内存,因此您的代码可能很慢。尝试重用该单元,而不必每次都创建一个新单元。
var cell = tableView.dequeueReusableCellWithIdentifier("YourCellName") as YourCellName!
if cell == nil {
cell = YourCellName(style: UITableViewCellStyle.Default, reuseIdentifier: "YourCellName")
}
以上代码仅在必要时创建一个新代码。否则,它将始终重用不可见的单元格。
希望这可以帮助.. :)