这是我的自定义表格视图单元格,我想每 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;
}