gpt4 book ai didi

ios - 加载 UITableView 后停止事件指示器

转载 作者:搜寻专家 更新时间:2023-11-01 06:07:06 25 4
gpt4 key购买 nike

如何在按下刷新按钮并在 UITableview JSON 中完成内容加载后自动停止事件指示器

我已经有了开始旋转的代码和按钮

完整代码如下

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var json_data_url = "http://www.howtotechworld.com/json_table_view_images%20(1).json"
var image_base_url = ""
var isProgressShowing = true;

var TableData:Array< datastruct > = Array < datastruct >()

enum ErrorHandler:ErrorType
{
case ErrorFetchingResults
}


struct datastruct
{
var imageurl:String?
var description:String?
var image:UIImage? = nil

init(add: NSDictionary)
{
imageurl = add["url"] as? String
description = add["description"] as? String
}
}

@IBOutlet var tableview: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

tableview.dataSource = self
tableview.delegate = self

get_data_from_url(json_data_url)
}

override func viewWillAppear(animated: Bool) {
let barButtonItem = UIBarButtonItem(title: "Refresh", style: .Plain, target: self, action: "refreshTapped");
self.navigationItem.rightBarButtonItem = barButtonItem;
}

func refreshTapped() {
addProgressIndicator(isProgressShowing);
get_data_from_url(json_data_url)
}

func addProgressIndicator(show : Bool) {
isProgressShowing = !show;
if(show) {
let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.Gray)
myActivityIndicator.startAnimating()
let barButtonItem = UIBarButtonItem(customView: myActivityIndicator)
self.navigationItem.rightBarButtonItem = barButtonItem
} else {
self.navigationItem.rightBarButtonItem = nil;
}




}




func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

let data = TableData[indexPath.row]


cell.textLabel?.text = data.description

if (data.image == nil)
{
cell.imageView?.image = UIImage(named:"image.jpg")
load_image(image_base_url + data.imageurl!, imageview: cell.imageView!, index: indexPath.row)
}
else
{
cell.imageView?.image = TableData[indexPath.row].image
}

return cell

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return TableData.count
}







func get_data_from_url(url:String)
{


let url:NSURL = NSURL(string: url)!
let session = NSURLSession.sharedSession()

let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData


let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in

guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
return
}

dispatch_async(dispatch_get_main_queue(), {
self.extract_json(data!)
return
})

}

task.resume()

}


func extract_json(jsonData:NSData)
{
let json: AnyObject?
do {
json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: [])
} catch {
json = nil
return
}

if let list = json as? NSArray
{
for (var i = 0; i < list.count ; i++ )
{
if let data_block = list[i] as? NSDictionary
{

TableData.append(datastruct(add: data_block))
}
}

do
{
try read()
}
catch
{
}

do_table_refresh()

}


}




func do_table_refresh()
{
dispatch_async(dispatch_get_main_queue(), {
self.tableview.reloadData()
return
})
}


func load_image(urlString:String, imageview:UIImageView, index:NSInteger)
{

let url:NSURL = NSURL(string: urlString)!
let session = NSURLSession.sharedSession()

let task = session.downloadTaskWithURL(url) {
(
let location, let response, let error) in

guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
print("error")
return
}

let imageData = NSData(contentsOfURL: location!)

dispatch_async(dispatch_get_main_queue(), {


self.TableData[index].image = UIImage(data: imageData!)
self.save(index,image: self.TableData[index].image!)

imageview.image = self.TableData[index].image
return
})


}

task.resume()


}




func read() throws
{

do
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName: "Images")

let fetchedResults = try managedContext.executeFetchRequest(fetchRequest)

for (var i=0; i < fetchedResults.count; i++)
{
let single_result = fetchedResults[i]
let index = single_result.valueForKey("index") as! NSInteger
let img: NSData? = single_result.valueForKey("image") as? NSData

TableData[index].image = UIImage(data: img!)

}

}
catch
{
print("error")
throw ErrorHandler.ErrorFetchingResults
}

}

func save(id:Int,image:UIImage)
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!

let entity = NSEntityDescription.entityForName("Images",
inManagedObjectContext: managedContext)
let options = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext:managedContext)

let newImageData = UIImageJPEGRepresentation(image,1)

options.setValue(id, forKey: "index")
options.setValue(newImageData, forKey: "image")

do {
try managedContext.save()
} catch
{
print("error")
}

}


}

最佳答案

您需要保留对 myActivityIndi​​cator 的引用并在 do_table_refresh() 中停止它,如下所示。

func do_table_refresh()
{
dispatch_async(dispatch_get_main_queue(), {
self.tableview.reloadData()

myActivityIndicator.stopAnimating()
return
})
}

编辑:在查看问题中的代码后快速想到。

如果您想使用 addProgressIndicator(isProgressShowing) 隐藏 myActivitIndicator ,您必须确保 isProgressShowing 设置为 FALSE,然后在我上面给出的代码中,将 myActivityIndi​​cator.stopAnimating() 替换为 addProgressIndicator(isProgressShowing)

您还需要更新 addProgressIndicator 函数并将 myActivityIndi​​cator.stopAnimating() 添加到 else 条件。

关于ios - 加载 UITableView 后停止事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34782762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com