从其他方法更新UITableViewCell

Truong Hong Quyen

我已经在GG中搜索了数千以查找解决方案更新数据到UITableViewCell,但是所有显示的解决方案是

UITableViewCell *cell=(UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];

但对于所有可见的单元格,该单元格均为零。我已经使用NSNotification将数据从一种方法发送到ViewController.m,并且Reiever方法我想通过indexPath将数据更新到单元格。但所有单元格均为零,因此无法更新。

这是我的代码ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) IBOutlet UITableView *tableView;

@end 

ViewController.m

@implementation ViewController
{

}

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

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

}                
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [recipes count];
}       
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        NSLog(@"cell nil");

    }

    NSString *idgame=@"Gamexyz";
    cell.textLabel.text = idgame;
    cell.tag=indexPath.row;

    return cell;
}

-(void)theReciever:(NSNotification*)notif{

    if([notif.object isKindOfClass:[packeData class]]){

        packeData *data=[notif object];

        NSString *key=data.key;
        NSInteger *index=[key integerValue];
        NSIndexPath *indexPath=[NSIndexPath indexPathWithIndex:index];

        UITableViewCell *cell=(UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];

       //UITableViewCell *cell=(UITableViewCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:7 inSection:0]];

        if(cell==nil)
        {
            NSLog(@"cell NULL");
        }else{
            cell.textLabel.text=data.process;
        }

    }else{
        NSLog(@"ERR: object not recognised");
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

任何人都可以帮助我或解决方案示例,以通过indexPath更新UITableViewCell中的数据

Shankar BS

注意:以下仅是示例,您可以在新项目中进行操作

有一件事u需要改变的数据模型packeData,可以说,它包含key作为NSIntager其holdes细胞的指数,processNSString其持有的进展,例如字符串值

packeData.h

#import <Foundation/Foundation.h>

@interface packeData : NSObject
@property (nonatomic, assign) NSInteger key; //holds index
@property (nonatomic, strong) NSString *process; //holds the progress info

@end

和在 packeData.m

#import "packeData.h"

@implementation packeData
- (id)init //simply initialise it 
{
   self = [super init];
   if(self)
   {

   }
   return self;
}

@结尾

在你是tableview的视图控制器中,

ViewController.h

#import <UIKit/UIKit.h>
#import "packeData.h"
@interface ViewController : UIViewController <UI TableViewDataSource,UITableViewDelegate>   
@property (weak, nonatomic) IBOutlet UITableView *aTableView;
@property (strong,nonatomic) NSMutableArray *recipes; //array acts as datasource

@end

在中 ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad  
{
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(theReciever:) name:@"theSender" object:nil];

  _recipes = [[NSMutableArray alloc]init]; //initilise your datasource
  for(int j = 0 ;j< 20;j++)
   {
    // for my example i took some values
    //initially put some initial values
    packeData *data = [[packeData alloc] init];
    data.key = j;
    data.process = [NSString stringWithFormat:@"game_name_%d",j];
    [_recipes addObject:data];
  }
 }

 - (void)viewDidAppear:(BOOL)animated
 {
   [super viewDidAppear:animated];
   [NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(PostNotification) userInfo:nil repeats:YES];  //just for testing   
 } 

 - (void)PostNotification
 {
   //i am simply posting the notification with some random values
   packeData *data = [[packeData alloc]init];
   data.key = arc4random()%15;
   data.process = [NSString stringWithFormat:@"%ld",( data.key  + 20)];
   [[NSNotificationCenter defaultCenter] postNotificationName:@"theSender" object:data];
 }

- (void)theReciever:(NSNotification *)notif
{
    if([notif.object isKindOfClass:[packeData class]]){

      packeData *data=[notif object];
      NSInteger key=data.key;
      NSInteger index= key;
      //modify the datasource
      packeData *recipes_data = [_recipes objectAtIndex:index]; //get the pocket present in array
      recipes_data.process = data.process; //modify the recipes data

      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
      UITableViewCell *cell=(UITableViewCell*)[self.aTableView cellForRowAtIndexPath:indexPath];

      if(cell==nil)
      {
         NSLog(@"cell NULL");
      }else
      {
         [self.aTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        //  cell.textLabel.text=data.process; no need u already mofied the content in the datasource this will call the "cellForRowAtIndexPath" method and displays the process in place of game name
     }
     }else{
       NSLog(@"ERR: object not recognised");
     }
 }

  - (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
  }

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

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

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   static NSString *simpleTableIdentifier = @"SimpleTableCell";
   UITableViewCell* cell = [self.aTableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    NSLog(@"cell nil"); 
   }
   packeData *idgame= [_recipes objectAtIndex:indexPath.row];
   cell.textLabel.text = idgame.process; //initially contains game name
   cell.tag=indexPath.row;
   return cell;
 }

 @end

编辑

替换以下方法

- (void)PostNotification
 {
   //i am simply posting the notification with some random values
   packeData *data = [[packeData alloc]init];
   data.key = arc4random()%15; //15 change the number of rows
   data.process = [NSString stringWithFormat:@"%ld",( data.key  + arc4random() % 100)];
   [[NSNotificationCenter defaultCenter]      postNotificationName:@"theSender" object:data];  
 }


 - (void)theReciever:(NSNotification *)notif
 {
    if([notif.object isKindOfClass:[packeData class]]){

    packeData *data=[notif object];
    NSInteger key=data.key;
    NSInteger index= key;
    //modify the datasource
    packeData *recipes_data = [_recipes objectAtIndex:index]; //get the pocket present in array
    recipes_data.process = data.process; //modify the recipes data

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    UITableViewCell *cell=(UITableViewCell*)[self.aTableView cellForRowAtIndexPath:indexPath];

    if(cell==nil)
    {
        NSLog(@"cell NULL");
        [self.aTableView reloadData]; //if cell is not visible then reload the whole table
    }else
    {
        [self.aTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
      //  cell.textLabel.text=data.process; no need u already mofied the content in the datasource this will call the "cellForRowAtIndexPath" method and displays the process in place of game name
    }
    }else{
      NSLog(@"ERR: object not recognised");
   }

 }

编辑2

至于测试,只需更改以下方法,然后模拟器启动应用程序向下滚动,以便仅更新前5行,等待5到10秒钟,然后滚动到顶部,您将看到所有调用都是同一过程的更新5

//scroll down as soon as launches the app and wait for 5 to 10 seconds then scroll to top u will see top 5 cells are updates with progress 5
- (void)PostNotification
{
   packeData *data = [[packeData alloc]init];
   data.key = arc4random()%5; //only top 5 cells are modify other wont modify
   data.process = [NSString stringWithFormat:@"%ld",5];//updates with some same progress lates give it as 5 //( data.key  + arc4random() % 100)];
  [[NSNotificationCenter defaultCenter] postNotificationName:@"theSender" object:data];
}

通过以上测试,即使看不见前5个单元格,它们也会保持更新

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章