سوال کلی در مورد داده ها در tableview
درود خدمت شما . متاسفانه تا وقتی ویدیو می بینیم که خوب پیش می ره ولی خوب سر پروژه جدید همه چیز می ریزه به هم و نیاز به کمک هست تا متورمون بلخره راه بیفته .
شما فکر کنید من اومدم اطلاعاتی رو از دیتابیس گرفتم . به شرح زیر :
let url = NSURL(string: "http://localhost:8888/note/service.php?action=select")
let setion = NSURLSession.sharedSession()
let task = setion.dataTaskWithURL(url!) { (NSData, response, error) in
do {
let records = try NSJSONSerialization.JSONObjectWithData(NSData!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
for record in records {
let not = StructNotes()
not.task_id = Int(record["task_id"] as! String)
not.task_title = record["task_title"] as! String
not.task_desc = record["task_desc"] as! String
not.task_done = Int(record["task_done"] as! String)
not.last_modify_time = record["last_modify_time"] as! String
self.notes.append(not)
}
}catch {
// cheack error
print("Json catch error")
} // end do try catch
}
task.resume()
}
خوب این کد همون کدی بود که که در آموزش ها از ویدیو گرفتیم و تغییر آنچنانی داخلش داده نشده .
خوب در کد های بالا اگر بیاییم به صورت مثال چاپ کنیم
not.task_done = Int(record["task_done"] as! String)
print(not.task_done)
خوب خروجی هرچی تایتل هست رو می یاره تا اینجا مشکلی نبود .
خوب برای اینکه بیاییم اون رو به یک آرایه بدیم اومدیم در بالای همین فایل یک پراپرتی ساختیم به صورت زیر :
internal var notes:[StructNote] = []
و اومدم یک فایل سو ایفتی با کلاس StructNotes ساختم . که موارد توش این هست که مثل آموزشه :
class StructNotes {
internal var task_id:Int!
internal var task_title:String!
internal var task_desc:String!
internal var task_done:Int!
internal var last_modify_time:String!
}
خوب همانطور که در کد بالا هم می بنید من اومدم اون رو append کردم به آرایه با خط زیر
self.notes.append(not)
طبق روال آموزش ها باید الان آرایه درست شده باشه بشه ازش استفاده کرد
من در اینجا مشکلم شروع می شود من در اینجا فکر می کنم در جایی کارم اشتباه بوده و این آرایه من نیل هست به همین منظور بدون اینکه هیچ کدی بزنم فقط در فانکشن مربوطه یعنی :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print(notes[indexPath.row])
برای من هیچی چاپ نمی شه حتی nil هم بر نمی گردونه . اینجاست که من نمی تونم هیچ چیزی رو نمایش بدم . کل کار من و چیزی که صحبت کردم فقط نمایش هست یعنی اطلاعاتی از سایت به واسطه دیتابیس گرفته می شود و از اون هم استفاده می شود برای نمایش توی یک lable همین .
با تشکر از اینکه وقت قرار دادید و این همه داستان منو خوندید فکر می کنم این مشکل بزرگ تا حل نشه ادامه راه زیاد جالب نیست . من اگر متغییر رو دستی بزارم خوب مشکلی نیست ولی خوب برنامه استاتیک نیست بلکه برنامه از یک وب سرویس ساده داره می خونه
کل کد :
import UIKit
class VCTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
internal var notes:[StructNotes] = []
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://localhost:8888/note/service.php?action=select")
let setion = NSURLSession.sharedSession()
let task = setion.dataTaskWithURL(url!) { (NSData, response, error) in
do {
let records = try NSJSONSerialization.JSONObjectWithData(NSData!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
for record in records {
let not = StructNotes()
not.task_id = Int(record["task_id"] as! String)
not.task_title = record["task_title"] as! String
not.task_desc = record["task_desc"] as! String
not.task_done = Int(record["task_done"] as! String)
not.last_modify_time = record["last_modify_time"] as! String
self.notes.append(not)
}
}catch {
// cheack error
print("Json catch error")
} // end do try catch
}
task.resume()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return notes.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let headerView = UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 30))
headerView.backgroundColor = UIColor.whiteColor()
return headerView
}
let cellSpce = 10
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
view.backgroundColor = UIColor.whiteColor()
tableView.backgroundColor = UIColor.clearColor()
return CGFloat(cellSpce)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print(notes[indexPath.row])
let cells = tableView.dequeueReusableCellWithIdentifier("TableViewVCWEbBlogs", forIndexPath: indexPath)
as! TableViewCellWebBlogs
cells.lableWebDesc.textAlignment = .Right
dispatch_async(dispatch_get_main_queue()) {
cells.uiimageWebImage.image = UIImage(named: "test")
// ui tableView
cells.labelWebTitle.font = UIFont(name: "IRANSansWeb", size: 11)
cells.lableWebDesc.font = UIFont(name: "IRANSansWeb", size: 11)
cells.layer.borderColor = UIColor.grayColor().CGColor
cells.layer.borderWidth = 0.5
cells.layer.cornerRadius = 2
// end ui tableView
cells.backgroundColor = UIColor.blackColor()
tableView.backgroundColor = UIColor.clearColor()
}
return cells
}
//move new page ContentWebBlogs
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let VcContent = storyboard?.instantiateViewControllerWithIdentifier("VcContent") as! ContentViewController
// VcContent.titleContent = String(number2[indexPath.row])
navigationController?.pushViewController(VcContent, animated: true)
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .