Swift_tableView创建及其实现协议


import Foundation
import UIKit

class RootViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { //协议

var tableView :UITableView? = UITableView( frame: CGRectZero, style: UITableViewStyle.Plain);


override func viewDidLoad() {
    configUI()
}
    func configUI(){
        tableView!.frame = CGRect (x: 0, y: 0, width: 320, height: 568)
        self.view.addSubview(tableView!)
        tableView!.delegate = self
        tableView!.dataSource = self //设置代理

    }
 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 10
}
 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var view:UIView = UIView (frame: CGRect (x: 0, y: 0, width: 300, height: 30))
    var la = UILabel (frame: view.frame);
    la.backgroundColor = UIColor.greenColor()
    la.text = "第\(section)+分区"  //分区的名字
    view.addSubview(la)
    return view;
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40 ; //每一个分组的高度
}
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cellId:String = "id"
    var cell = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell; //?是可选的 就是可能是nil
    if cell == nil {
        cell = UITableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: cellId)
    }
    cell?.textLabel?.text = String (indexPath.row )
    return cell!// 返回cell 或者 nil
}

}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if appContext.isCloudVMSLogin { if let message = self.getMessageBy(index: indexPath) { if (self.getMessageCount() >= 20) && (self.getMessageCount() - self.getMessageIndexBy(vmsMessageId: message.vmsMessageID) < 6) && !isRefreshing && !self.beLast && getSiteMessageForOneDayRequestCode < 0 { self.isRefreshing = true self.getNextPage() } } } } override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { if dataSource.isEmpty { return UIView() } let header = DeviceSharingSimpleHeaderView(frame: .zero) header.backgroundColor = .tpbBackground header.hintLabel.font = UIFont.tpr14Regular() header.hintLabel.textColor = UIColor.tpbTextSecondaryContent let message = dataSource[section][0] if appContext.isCloudVMSLogin { header.hintLabel.text = self.getDayString(timeInSection: message.creatTime/1000) } else { header.hintLabel.text = MessageManagerPublic.getDayTimeString(Double(message.devTimeInSecond)) + "(\(dataSource[section].count))" } header.accessibilityTraits = .header header.accessibilityLabel = String(format: "%@, %@", MessageManagerPublic.getDayTimeString(Double(message.devTimeInSecond)), String(format: LocalizedString(key: adaMessages), dataSource[section].count)) header.translatesAutoresizingMaskIntoConstraints = false let view = UIView(frame: .zero) view.backgroundColor = .clear view.addSubview(header) [ header.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16), header.topAnchor.constraint(equalTo: view.topAnchor), header.bottomAnchor.constraint(equalTo: view.bottomAnchor), header.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16) ].forEach({ $0.isActive = true }) return view } override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { if isVMS { return UITableViewCell.EditingStyle.none } if editingModel != nil { return UITableViewCell.EditingStyle.none } else { return UITableViewCell.EditingStyle.delete } } override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? { if isVMS { return "" } return LocalizedString(key: messageManagerDelete) } override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { //左滑删除单条Cell //vms暂无删除消息功能 if !(isVMS) { if editingStyle == .delete { deleteMessage(at: indexPath) } } } /* 如果图片移出屏幕且还没下载完成,则停止下载 */ override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { if let cell = cell as? MessageCenterNewMessageCell { if cell.messageImageRequestID > 0 { if isVMS { cancelDownloadResourceForVmsMessage(with: cell.messageImageRequestID, at: indexPath) cell.messageImageRequestID = 0 } else { cancelDownloadResource(with: cell.messageImageRequestID, llDeviceId: UInt64(cell.llDeviceId)) cell.messageImageRequestID = 0 } } if isVMS { if let _ = self.getMessageBy(index: indexPath) { //TODO: VMs消息类型区分 if isLoadingVisibleCell && isScroll { cell.imageStatus = .downloading } } } else { if let _ = self.getMessageBy(index: indexPath){ //针对事件消息,滑动中做图片加载处理 if(isLoadingVisibleCell && isScroll /* && message.messageType == .event && device.deviceType != .solar */){ cell.imageStatus = .downloading } } } } } }这些重写方法都是干什么的
10-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值