2015-09-02 65 views
0

我希望你們能夠幫助我在我的代碼中加載來自網站的數據。讓我們先從代碼:TableView沒有更新

-(void)viewDidLoad  
{ 
    [self loadDataFromUrl];   
} 

-(void)loadDataFromUrl { 

    NSString *myUrl = [NSString stringWithFormat:@"http://WebSite/PhpFiles/File.php"]; 

    NSURL *blogURL = [NSURL URLWithString:myUrl]; 
    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL]; 
    NSError *error = nil; 
    NSDictionary *dataDictionary = [NSJSONSerialization 
            JSONObjectWithData:jsonData options:0 error:&error]; 

    for (NSDictionary *bpDictionary in dataDictionary) { 

     HotelObject *currenHotel = [[HotelObject alloc]initWithVTheIndex:     
      [bpDictionary objectForKey:@"TheIndex"] timeLineVideoUserName: 
      [bpDictionary objectForKey:@"timeLineVideoUserName"] timeLineVideoDetails: 
      [bpDictionary objectForKey:@"timeLineVideoDetails"] timeLineVideoDate: 
      [bpDictionary objectForKey:@"timeLineVideoDate"] timeLineVideoTime: 
      [bpDictionary objectForKey:@"timeLineVideoTime"] timeLineVideoLink: 
      [bpDictionary objectForKey:@"timeLineVideoLink"] timeLineVideoLikes: 
      [bpDictionary objectForKey:@"timeLineVideoLikes"] videoImage: 
      [bpDictionary objectForKey:@"videoImage"] timeDeviceToken: 
      [bpDictionary objectForKey:@"deviceToken"]]; 

      [self.objectHolderArray addObject:currenHotel]; 

    }  

} 

-(NSMutableArray *)objectHolderArray{ 
    if(!_objectHolderArray) _objectHolderArray = [[NSMutableArray alloc]init]; 
    return _objectHolderArray; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: 
(NSInteger)section 
{ 
    return [self.objectHolderArray count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                 forIndexPath:indexPath]; 
    HotelObject *currentHotel = [self.objectHolderArray 
           objectAtIndex:indexPath.row]; 
    cell.lblID.text = currentHotel.vvTheIndex; 
    cell.lblName.text = currentHotel.vvUserName; 
    cell.lblCity.text = currentHotel.vvVideoDate; 
    cell.lblAddress.text = currentHotel.vvVideoLikes; 
    return cell; 
} 

上面的代碼從URL加載數據,並填補了TableView中,這一步工作的罰款。該應用程序有另一個ViewController,它是DetailsView,在DetailsView中用戶可以更新一些信息。當用戶返回到TableView時什麼都沒有發生(我的意思是數據仍然是一樣的,沒有更新)。我試圖從ViewDidAppear調用loadDataFromurl,但它不起作用。

我做了補充:

[self.tableView reloadData]; 

但還是同樣的結果。

她也是我的NSObject的代碼:

-(instancetype)initWithVTheIndex:(NSString *)vTheIndex  
      timeLineVideoUserName:(NSString *)vUserName 
      timeLineVideoDetails:(NSString *)vVideoDetails 
      timeLineVideoDate:(NSString *)vVideoDate 
      timeLineVideoTime:(NSString *)vVideoTime 
      timeLineVideoLink:(NSString *)vVideoLink 
      timeLineVideoLikes:(NSString *)vVideoLikes 
      videoImage:(NSString *)vVideoImage 
      timeDeviceToken:(NSString *)vDeviceToken { 


    self = [super init]; 
    if(self){ 
     self.vvTheIndex = vTheIndex; 
     self.vvUserName = vUserName; 
     self.vvVideoDetails = vVideoDetails; 
     self.vvVideoDate = vVideoDate; 
     self.vvVideoTime = vVideoTime; 
     self.vvVideoLink = vVideoLink; 
     self.vvVideoLikes = vVideoLikes; 
     self.vvVideoImage = vVideoImage; 
     self.vvDeviceToken = vDeviceToken; 

    } 
    return self; 
} 

我的問題是:如何從DetailsView控件移動,甚至當我拉刷新時更新的TableView。

在此先感謝

回答

0

您沒有正確地重新加載您的UITableView的數據源。

假設遠程URL正在更新新數據,並且問題只是重新加載TableView,請確保您添加了[self.tableView reloadData] AFTER loadDataFromUrl已完成。

reloadData方法將始終刷新表格的內容,所以它不會在正確的時間被調用,或者您沒有更新self.objectHolderArray。 (您可以在更新後通過調用loadDataFromUrl後設置斷點來進行調試。)

+0

謝謝Meriw,我已經知道了 – user3741700

0

on viewDidLoad生命週期第一次[self.tableView reloadData] reload tableview then then work。