- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我是 IOS
swift
开发新手。我曾经使用以前的 Xcode 6 beta
。
我已经下载了 Xcode 6.0.1
但我无法让它工作 Xcode Version: 6.0.1
当我尝试运行示例时,我仍然收到“'MyViewController
' does not confirm to protocol 'UITableViewDataSource
'”。
有人可以帮帮我吗?我已经解决了这个网站上的其他问题,并为“UITableViewDataSource
”添加了所有必需的功能;
import UIKit
import Foundation
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
var array1:[String] = ["one","two","three","four"]
var array2:[String] = ["IOS","Android","java","c++","Swift"]
let sectionCount = 2
var myTableView:UITableView!
// init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
// var rect = CGRectMake(0, 0, 220, 320)
// myTableView = UITableView(frame: rect, style: UITableViewStyle.Grouped)
// super.init(nibName: nil, bundle: nil)
// // Custom initialization
// }
override func viewDidLoad() {
super.viewDidLoad()
var rect = CGRectMake(0, 0, 320, 600)
myTableView = UITableView(frame: rect, style: UITableViewStyle.Grouped)
myTableView!.delegate = self
myTableView!.dataSource = self
self.view.addSubview(myTableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//dataSourrce
//tableview:tableview,section:
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
switch section{
case 0:
return array1.count
case 1:
return array2.count
default:
return 1
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
//---cellstart----
let identifier = "identifier"
// var cell:UITableViewCell
//cell
var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? MyCell
if cell == nil {
// cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: identifier)
cell = MyCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: identifier)
}
//---cellend----
switch indexPath.section{
case 0:
// cell!.textLabel.text = array1[indexPath.row]
cell!.myLable!.text = array1[indexPath.row]
case 1:
// cell!.textLabel.text = array2[indexPath.row]
cell!.myLable!.text = array2[indexPath.row]
default:
println()
}
var image = UIImage(named: "images/qq.png")
// cell!.imageView.image = image
cell!.myImageView!.image = image
// cell!.detailTextLabel.text = "\(indexPath.section)\(indexPath.row)
return cell!
}
//dataSourrce
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return sectionCount
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
var title:String? = nil
switch section {
case 0:
title = "Num"
case 1:
title = "Prog"
default:
title = nil
}
return title
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
println("Test\(indexPath.section) \(indexPath.row)")
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60.0
}
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {}
func pickerView(pickerView: UIPickerView!,numberOfRowsInComponent component: Int) -> Int{}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?{
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?{
}
}
***************** 我的细胞类 *************************** ********
import Foundation
import UIKit
class MyCell: UITableViewCell {
let indetifier:String = "indetifier"
var myLable:UILabel?
var myImageView:UIImageView?
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
super.init(style: .Subtitle, reuseIdentifier: indetifier)
var rect = CGRectMake(10, 0, 60, 30)
self.myLable = UILabel()
self.myLable!.frame = rect
self.myLable!.textColor = UIColor.redColor()
self.contentView.addSubview(self.myLable!)
var imageRect = CGRectMake(160, 10, 40, 40)
self.myImageView = UIImageView()
self.myImageView!.frame = imageRect
self.contentView.addSubview(self.myImageView!)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
最佳答案
您需要查看整个错误消息。此特定消息包含有关缺少哪些方法的附加信息:
Type 'MyViewController' does not conform to protocol 'UITableViewDataSource'
Protocol requires function 'tableView(_:numberOfRowsInSection:)' with type '(UITableView, numberOfRowsInSection: Int) -> Int'
Candidate has non-matching type '(UITableView!, numberOfRowsInSection: Int) -> Int'
所以...您的 numberOfRowsInSection
采用可选的 UITableView
,并且应该采用必需的 UITableView
(这是他们在 6 和 6.1 之间所做的更改,所有 UITableView
委托(delegate)和数据源方法现在都采用必需的 tableView
和 indexPath
值)
关于iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25935848/
有没有办法检查 MyViewController 是否在窗口层次结构中?我想知道我是否可以从中呈现一个 UIAlertController : var alert = UIAlertControlle
我最近从developer.apple.com 下载了PhotoPicker 示例代码应用程序,因为我对iOS 还比较陌生。 为什么在其他代码或 Interface Builder nib 中的任何地
我的 View Controller 的名称是RouteToStationViewController,在这个 View Controller 中,我在尝试编译时收到警告,后来导致崩溃,我的相关代码是
我尝试创建类似 SDK 的东西 - 默认 View 包、开发人员可以扩展和自定义的 Controller 如果用户的 View 不是以编程方式生成,而是在单独的 .storyboard 中创建,那么我
我是 IOS swift 开发新手。我曾经使用以前的 Xcode 6 beta。 我已经下载了 Xcode 6.0.1 但我无法让它工作 Xcode Version: 6.0.1 当我尝试运行示例时,
我正在我的类上创建一个扩展以符合协议(protocol)“STPPaymentContextDelegate”。出于某种原因,编译器会报错,即使我已经像这样正确声明了扩展中的所有方法。 extensi
我有一个具有两个本地化版本的应用程序。通常我使用“NSLocalizedString”并为两种本地化保留相同的源代码,但我的一个 ViewControllers 必须在每个区域设置中具有不同的外观和功
当我尝试更改 .h 和 .m 中的 View Controller 名称时,出现此错误: Lexical or PreProcessor Issue 'MyViewController.h' file
当我尝试执行 self.tv_salaryNumber.delegate = self 时,不确定为什么会出现此错误(此问题的标题是错误,如下面的屏幕截图所示)。 我的类 EarningsVC 实际上
我刚刚在 Swift 中创建了一个新类,它叫做 myViewController,它是一个 UIViewController。现在,我试图将其设为 UIPickerViewDelegate 和 Dat
我试图理解使用初始化程序初始化 UINavigationController 背后的逻辑: UINavigationController(rootViewController: MyViewContr
众所周知的错误,但奇怪的情况: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setVal
我正在尝试使用此代码连接到我的 ViewController: UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone
我是一名优秀的程序员,十分优秀!