我想在UITableView
内显示用户列表。我构建它后出现了这个错误。
我创建了一个数据模型来存储用户列表。
CRUDSwiftData.swift
import UIKit
class CRUDSwiftData: NSObject {
var title: String
var rating: Float
init(title: String,rating: Float) {
self.title = title
self.rating = rating
}
}
CRUDSwiftDoc.swift
import UIKit
class CRUDSwiftDoc: NSObject {
var data:CRUDSwiftData
var thumbImage:UIImage
var fullImage:UIImage
init(title: String,rating: Float,data:CRUDSwiftData, thumbImage:UIImage, fullImage:UIImage) {
self.data = CRUDSwiftData(title: title,rating: rating);
self.thumbImage = thumbImage
self.fullImage = fullImage
}
}
然后我将我的MasterViewController.swift
课程编辑到此
MasterViewController.swift
var users: NSMutableArray = []
// At the end of viewDidLoad
self.title = "User List";
// Replace the return statement in tableView:numberOfRowsInSection with the following:
return users.count
// Replace tableView:cellForRowAtIndexPath with the following
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"MyBasicCell"];
let user = users[indexPath.row] as CRUDSwiftDoc
cell.textLabel.text = user.data.title
cell.imageView.image = user.thumbImage
return cell;
}
最后我的AppDelegate.swift
就像这样
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
let user1 = CRUDSwiftDoc(title:"Potato Bug", rating: "4", thumbImage: "potatoBugThumb.jpg", fullImage: "potatoBug.jpg")
let user2 = CRUDSwiftDoc(title:"House Centipede", rating: "3", thumbImage: "centipedeThumb.jpg", fullImage: "centipede.jpg")
let user3 = CRUDSwiftDoc(title:"Wolf Spider", rating: "5", thumbImage: "wolfSpiderThumb.jpg", fullImage: "wolfSpider.jpg")
let user4 = CRUDSwiftDoc(title:"Lady Bug", rating: "1", thumbImage: "ladybugThumb.jpg", fullImage: "ladybug.jpg")
let shoppingList: NSMutableArray[] = [user1, user2, user3, user4]
return true
}
AppDelegate.swift
请建议。
答案 0 :(得分:2)
编译器说实话!你在打电话
let user1 = CRUDSwiftDoc(title:"Potato Bug", rating: "4", thumbImage: "potatoBugThumb.jpg", fullImage: "potatoBug.jpg")
但您的声明需要
init(title: String, rating: Float, data:CRUDSwiftData, thumbImage:UIImage, fullImage:UIImage) {
更新的 您需要通过init将数据传递给CRUDSwiftDoc(),但是A)您没有传入它,而B)您正在计算它作为init。您需要将init更改为
class CRUDSwiftDoc: NSObject {
var data:CRUDSwiftData
var thumbImage:UIImage
var fullImage:UIImage
init(title: String, rating: Float, thumbImage:UIImage, fullImage:UIImage) { // No data!!!
self.data = CRUDSwiftData(title: title,rating: rating);
self.thumbImage = thumbImage
self.fullImage = fullImage
}
}
另请注意,您将评级作为字符串传递(评级:" 4"),而不是声明的Float。
答案 1 :(得分:0)
你的CRUDSwiftDoc类有这样的初始化:
init(title: String,rating: Float,data:CRUDSwiftData, thumbImage:UIImage, fullImage:UIImage)
第3个参数是数据但是在
中初始化4个对象时func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
您没有提供数据参数:
let user1 = CRUDSwiftDoc(title:"Potato Bug", rating: "4", thumbImage: "potatoBugThumb.jpg", fullImage: "potatoBug.jpg")
您必须在rating和thumbImage之间添加数据。
你也将字符串传递给rating参数但是它需要float并且你再次将字符串传递给thumbImage和fullImage,但它需要UIImage。将其更改为:
let data1 = CRUDSwiftData(title: "title 1", rating: 2.1);
let user1 = CRUDSwiftDoc(title:"Potato Bug", rating: 4.0, data:data1, thumbImage: UIImage(named:"potatoBugThumb.jpg"), fullImage: UIImage(named:"potatoBug.jpg"))
您遇到的最后一个问题是您创建了NSMUtableArray数组但是传递了CRUDSwiftDoc对象的数组。用以下代码替换您的代码:
let shoppingList:CRUDSwiftDoc [] = [user1,user2,user3,user4]