我遇到了Swift和Xcode 7的问题。
log
使用此代码,我收到以下错误:
致命错误:数组索引超出范围
在以下行中:
class ConnectVC: UITableViewController {
var username:Array< String > = Array < String >()
var TableData:Array< String > = Array < String >()
var pictures:Array< String > = Array < String >()
var profile_pictures:Array< String > = Array < String >()
override func viewDidLoad() {
super.viewDidLoad()
get_data_from_url("-url-")
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ConnectVCCell
let picture = pictures[indexPath.row]
print(pictures.count)
print(picture)
print(profile_pictures.count)
let pic = profile_pictures[indexPath.row]
if picture != "" {
let aString = "-url-"
let url = NSURL(string: aString)
let data = NSData(contentsOfURL: url!)
print(url)
let image = UIImage(data: data!)
cell.imageURL.image = image
}else{
print("No picture")
cell.imageURL.image = nil
}
cell.mainLabel.text = TableData[indexPath.row]
return cell
}
func get_data_from_url(url:String)
{
let url = NSURL(string: url)
let urlRequest = NSMutableURLRequest(URL: url!,
cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 15.0)
let queue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(
urlRequest,
queue: queue,
completionHandler: {response, data, error in
if data!.length > 0 && error == nil{
let json = NSString(data: data!, encoding: NSASCIIStringEncoding)
self.extract_json(json!)
}else if data!.length == 0 && error == nil{
print("Nothing was downloaded")
} else if error != nil{
print("Error happened = \(error)")
}
}
)
}
func extract_json(data:NSString)
{
let jsonData:NSData = data.dataUsingEncoding(NSASCIIStringEncoding)!
do {
// Try parsing some valid JSON
let json: AnyObject? = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments)
let data_list = json as? NSArray
for (var i = 0; i < data_list!.count ; i++ )
{
if let data_obj = data_list![i] as? NSDictionary
{
if let text = data_obj["text"] as? String
{
if let picture = data_obj["picture"] as? String
{
if let user = data_obj["user"] as? String
{
self.save_image("-url-")
TableData.append(text + " [" + user + "]")
pictures.append(picture)
}
}
}
}
}
}
catch let error as NSError {
// Catch fires here, with an NSErrro being thrown from the JSONObjectWithData method
print("A JSON parsing error occurred, here are the details:\n \(error)")
}
do_table_refresh();
}
func save_image(url:String){
let url = NSURL(string: url)
let urlRequest = NSMutableURLRequest(URL: url!,
cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 15.0)
let queue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(
urlRequest,
queue: queue,
completionHandler: {response, data, error in
if data!.length > 0 && error == nil{
let json = NSString(data: data!, encoding: NSASCIIStringEncoding)
self.extract_json_picture(json!)
}else if data!.length == 0 && error == nil{
print("Nothing was downloaded")
} else if error != nil{
print("Error happened = \(error)")
}
}
)
}
func extract_json_picture(data:NSString)
{
let jsonData:NSData = data.dataUsingEncoding(NSASCIIStringEncoding)!
do {
// Try parsing some valid JSON
let json: AnyObject? = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments)
print(json)
let user_info = json as? NSArray
if let user_list = user_info![0] as? NSDictionary
{
if let profile_picture = user_list["picture"] as? String
{
profile_pictures.append(profile_picture)
}
}
}
catch{
print("A JSON parsing error occurred, here are the details:\n \(error)")
}
问题是,数组是空的。但我没有看到问题。我认为正确调用填充数组的函数。
有人可以帮忙吗?
答案 0 :(得分:1)
`tableViewcellForRowAtIndexPath:在TableData有数据之前被调用。
答案 1 :(得分:0)
确保您的部分行数返回图片数量:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profile_pictures.count
}
这可以防止在该索引处不存在图片时创建单元格。