1

在此处输入图像描述

这是我的自定义表格视图单元格,我想每 1 分钟更改一次这些值(值将从服务器加载)。我已经设置了一个 Nstimer 并尝试重新加载 tableview。我的数组中的数据变化很好,但 uitableviewcell 中的值没有变化。

代码:

表视图控制器.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

视图控制器.h

@interface Viewcontroller : UIViewController
{

   Tableviewcontroller *Tablecontroller;
       IBOutlet UITableView *myTable;
}

@end

视图控制器.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;



 }
4

5 回答 5

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。

于 2013-06-19T08:05:39.657 回答
0

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController

@end

视图控制器.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];


}

请尝试此代码,如果您遇到任何问题,请告诉我。谢谢

于 2013-06-19T06:56:30.993 回答
0

你为什么不试试

[self.tableView reloadData];

代替

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

仅当您的超类是 UITableViewController 时。如果不是,则为 tableView 创建一个出口并通过它重新加载表。

你用过这个'setNeedsDisplay'作为

[cell.data11 setNeedsDisplay] if cellForRow is calling.
于 2013-06-19T07:51:46.767 回答
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 重新加载数据。

于 2013-06-19T09:36:59.567 回答
0

我解决了这个问题。忘记在 viewcontroller 中重新加载 UITableview 对象。我从 Tableviewcontroller.m 中删除了 NStimer 并将其放在 Viewcontroller.m 中。

修改后的代码:

表视图控制器.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

视图控制器.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];



}

谢谢大家帮助我。

于 2013-06-19T09:52:25.773 回答