2013-06-19 60 views
1

enter image description here定製的tableview細胞不清爽

這是我自定義表格視圖單元格,我想改變在每1分鐘的值(值將從服務器加載)。我已經設置了Nstimer並嘗試重新加載tableview。我陣列中的數據正在改變,但uitableviewcell中的值沒有變化。

代碼:

Tableviewcontroller.m

-(void) loadView 
{ 

parsejson = [ParseJson alloc]; 
defaults=[NSUserDefaults standardUserDefaults]; 

items=[NSArray arrayWithObjects:@" data1",@" data2 ", nil]; 
usercount=[NSArray arrayWithObjects: 
[parsejson getdata:[defaults valueForKey:@"userid"]],@" 0 ", nil]; 




    listtimer = [NSTimer scheduledTimerWithTimeInterval:60.0    // start timer (interval 2 secs) 
               target:self 
              selector:@selector(reloadlist:) 
              userInfo:nil 
              repeats:YES]; 


} 

- (void)reloadlist:(NSTimer*)timer 
{ 
    NSLog(@"list reload "); 

    items=[NSArray arrayWithObjects:@" data1",@" data2 ", nil]; 
    usercount=[NSArray arrayWithObjects: 
    [parsejson getdata:[defaults valueForKey:@"userid"]],@" 0 ", nil]; 


    //UITableView *tv = (UITableView *)self.view; 
    [self.tableview reloadData]; 

    // NSIndexPath *a = [NSIndexPath indexPathForRow:0 inSection:0]; // I wanted to update this cell specifically 
    // NearestListCell *c = (NearestListCell *)[tv cellForRowAtIndexPath:a]; 
    //c.count=[usercount objectAtIndex:0]; 
    //[tv beginUpdates]; 
    //// [tv reloadRowsAtIndexPaths:usercount withRowAnimation:UITableViewRowAnimationRight]; 
    //[tv endUpdates]; 
    // [tv reloadData]; 
} 



#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [usercount count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"NearmeListIdentifier"; 

    listCell *cell = (listCell *)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"listCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.text.text = [items objectAtIndex:indexPath.row]; 
    cell.data11.text = [usercount objectAtIndex:indexPath.row]; 
    cell.data22.text = @" 0.0 "; 

    return cell; 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 

@end 

Viewcontroller.h

@interface Viewcontroller : UIViewController 
{ 

    Tableviewcontroller *Tablecontroller; 
     IBOutlet UITableView *myTable; 
} 

@end 

Viewcontroller.m

-(void)viewDidLoad 
{ 

    [super viewDidLoad]; 


    defaults = [NSUserDefaults standardUserDefaults]; 

    if (Tablecontroller == nil) 
    { 
     Tablecontroller = [[Tableviewcontroller alloc] init]; 
} 
     [myTable setDataSource:Tablecontroller]; 

      [myTable setDelegate:Tablecontroller]; 

    Tablecontroller.view = Tablecontroller.tableView; 



} 
+0

嘗試[self.tableView reloadData];而不是UITableView * tv =(UITableView *)self.view; [電視reloadData]; – iCoder

+0

是[self.myTable reloadData]而不是UITableView * tv =(UITableView *)self.view; [tv reloadData];應該爲你工作 – channi

+0

我已經嘗試[self.tableView reloadData]但沒有什麼改變 –

回答

0

我解決了這個問題。忘記在viewcontroller中重新加載UITableview對象。我從Tableviewcontroller.m中移除NStimer,並將其放置在Viewcontroller.m中。

更改後的代碼:

TableViewController.m

-(void) loadView 
{ 

parsejson = [ParseJson alloc]; 
defaults=[NSUserDefaults standardUserDefaults]; 

items=[NSArray arrayWithObjects:@" data1",@" data2 ", nil]; 
usercount=[NSArray arrayWithObjects: 
[parsejson getdata:[defaults valueForKey:@"userid"]],@" 0 ", nil]; 

    **// removed the nstimer from here.** 

} 


#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [usercount count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

// Added these 2 lines of code here. 
    items=[NSArray arrayWithObjects:@" data1",@" data2 ", nil]; 
    usercount=[NSArray arrayWithObjects:[parsejson getNearestUsercount:[defaults valueForKey:@"userid"]],@" 0 ", nil]; 


    static NSString *CellIdentifier = @"NearmeListIdentifier"; 

    listCell *cell = (listCell *)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"listCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.text.text = [items objectAtIndex:indexPath.row]; 
    cell.data11.text = [usercount objectAtIndex:indexPath.row]; 
    cell.data22.text = @" 0.0 "; 

    return cell; 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 

@end 

Viewcontroller.m

-(void)viewDidLoad 
{ 

    [super viewDidLoad]; 


    defaults = [NSUserDefaults standardUserDefaults]; 

    if (Tablecontroller == nil) 
    { 
     Tablecontroller = [[Tableviewcontroller alloc] init]; 
} 
     [myTable setDataSource:Tablecontroller]; 

      [myTable setDelegate:Tablecontroller]; 

    Tablecontroller.view = Tablecontroller.tableView; 

// I Put the Nstimer here. 

aTimer = [NSTimer scheduledTimerWithTimeInterval:2.0    // start timer (interval 2 secs) 
               target:self 
              selector:@selector(timerTicked:) 
              userInfo:nil 
              repeats:YES]; 


} 

- (void)timerTicked:(NSTimer*)timer 
{ 
    [myTable.self reloadData]; 



} 

謝謝大家對我的幫助。

0

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UITableViewController 

@end 

ViewController.m

#import "ViewController1.h" 

    @implementation ViewController 

    - (id)initWithStyle:(UITableViewStyle)style 
    { 
     self = [super initWithStyle:style]; 
     if (self) { 

     } 
     return self; 
    } 

    - (void)viewDidLoad 
    { 
    [super viewDidLoad]; 

    } 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
    return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
    return [usercount count]; 
    } 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"NearmeListIdentifier"; 

     listCell *cell = (listCell *)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"listCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 

    cell.text.text = [items objectAtIndex:indexPath.row]; 
    cell.data11.text = [usercount objectAtIndex:indexPath.row]; 
    cell.data22.text = @" 0.0 "; 
    return cell; 
    } 

- (void)reloadlist:(NSTimer*)timer 
{ 
    NSLog(@"list reload "); 

    items=[NSArray arrayWithObjects:@" data1",@" data2 ", nil]; 
    usercount=[NSArray arrayWithObjects: 
    [parsejson getdata:[defaults valueForKey:@"userid"]],@" 0 ", nil]; 
    [self.tableView reloadData]; 
} 


-(void) loadView 
{ 

parsejson = [ParseJson alloc]; 
defaults=[NSUserDefaults standardUserDefaults]; 

items=[NSArray arrayWithObjects:@" data1",@" data2 ", nil]; 
usercount=[NSArray arrayWithObjects: 
[parsejson getdata:[defaults valueForKey:@"userid"]],@" 0 ", nil]; 




    listtimer = [NSTimer scheduledTimerWithTimeInterval:60.0    // start timer (interval 2 secs) 
               target:self 
              selector:@selector(reloadlist:) 
              userInfo:nil 
              repeats:YES]; 


} 

請試試這個代碼,並讓

+0

我想要一個單獨的類文件作爲tableviewcontroller,因爲,同一屏幕包含多個tableview –

0

你爲什麼不試試我知道,如果你面對任何problems.Thanks

[self.tableView reloadData]; 

代替的

UITableView *tv = (UITableView *)self.view; 
    [tv reloadData]; 

只有當你的超級類是UITableViewController.If不然,然後爲tableView創建一個出口並通過它重新加載表。

你用過這個「setNeedsDisplay」作爲

[cell.data11 setNeedsDisplay] if cellForRow is calling. 
+0

我已經嘗試過它,但沒有爲我工作 –

+0

問題是cellForRow不是調用 –

+0

哦,那麼需要深刻地看到它,第一次打電話? – Warewolf

1

請試試這個代碼:它的做工精細這裏

/* FirstTVContoller.h */ 

#import <Foundation/Foundation.h> 

@interface FirstTVContoller : UITableViewController <UITableViewDataSource, UITableViewDelegate>{ 
    NSMutableArray *items; 
} 

@end 


/* FirstTVContoller.m */ 

#import "FirstTVContoller.h" 
#import "SecondTVController.h" 

@implementation FirstTVContoller 


-(void) loadView 
{ 
    if (items == nil) { 
     items = [[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"6",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",nil] retain]; 
    } 
} 

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ 
    return [items count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"MyIdentifier"]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"1.%@" ,[items objectAtIndex:indexPath.row]]; 
    return cell; 
} 

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

     return UITableViewCellEditingStyleDelete; 

} 
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(editingStyle == UITableViewCellEditingStyleDelete) {  
     //Delete the object from the table. 
     [items removeObjectAtIndex:indexPath.row]; 
     [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

-(void) dealloc 
{ 
    [items release]; 
    [super dealloc]; 
} 

@end 





/* SecondTVController.h */ 




#import <Foundation/Foundation.h> 


@interface SecondTVController : UITableViewController <UITableViewDataSource, UITableViewDelegate>{ 
    int numberOfCells; 
} 

@end 


/* SecondTVController.m */ 

#import "SecondTVController.h" 


@implementation SecondTVController 
-(void) viewDidLoad 
{ 
    numberOfCells = 20; 
} 
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ 
    return numberOfCells; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"MyIdentifier"]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"2.%d", indexPath.row]; 

    return cell; 
} 

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return UITableViewCellEditingStyleDelete; 

} 
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(editingStyle == UITableViewCellEditingStyleDelete) {  
     //Delete the object from the table. 
     numberOfCells -=1; 
     [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
    } 
} 

@end 

/* TwoTableViewsViewController.h */ 


#import <UIKit/UIKit.h> 
#import "FirstTVContoller.h" 
#import "SecondTVController.h" 

@interface TwoTableViewsViewController : UIViewController{ 
    FirstTVContoller *firstController; 
    SecondTVController *secondController; 
    IBOutlet UITableView *firstTable; 
    IBOutlet UITableView *secondTable; 
} 

@end 


/* TwoTableViewsViewController.m */ 

#import "TwoTableViewsViewController.h" 

@implementation TwoTableViewsViewController 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    if (firstController == nil) { 
     firstController = [[FirstTVContoller alloc] init]; 
    } 
    if (secondController == nil) { 
     secondController = [[SecondTVController alloc] init]; 
    } 
    [firstTable setDataSource:firstController]; 
    [secondTable setDataSource:secondController]; 

    [firstTable setDelegate:firstController]; 
    [secondTable setDelegate:secondController]; 
    firstController.view = firstController.tableView; 
    secondController.view = secondController.tableView; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)dealloc { 
    [firstController release]; 
    [secondController release]; 
    [firstTable release]; 
    [secondTable release]; 
    [super dealloc]; 
} 

讓我知道如果你仍然面臨的任何問題。這將肯定適用於n-no。的UITableViews。

+0

其實我是通過參考上面的類來編碼的。這工作正常,但問題是tableview不重新加載。 –

0

請更改下面的代碼:

/* TwoTableViewsViewController.m */ 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    if (firstController == nil) { 
     firstController = [[FirstTVContoller alloc] init]; 
    } 
    if (secondController == nil) { 
     secondController = [[SecondTVController alloc] init]; 
    } 
    [firstTable setDataSource:firstController]; 
    [secondTable setDataSource:secondController]; 

    [firstTable setDelegate:firstController]; 
    [secondTable setDelegate:secondController]; 
    firstController.view = firstController.tableView; 
    secondController.view = secondController.tableView; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:@"ReloadFirstTable" object:nil]; 

} 

-(void)reloadData:(id)sender 
{ 
    [firstTable reloadData]; 
} 


/* FirstTVContoller.m */ 

- (void)reloadlist:(NSTimer*)timer 
{ 
    NSLog(@"list reload "); 
    self.items = [NSMutableArray arrayWithObjects:@"data1",@" data2 ", nil]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadFirstTable" object:nil]; 
} 

請使用NSNotificationCenter重新加載數據。

+0

我認爲這也會起作用。 –