【发布时间】:2015-09-09 04:31:52
【问题描述】:
我在 GG 中搜索了千元以找到 UITableViewCell 的解决方案更新数据,但都显示解决方案是
UITableViewCell *cell=(UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
但是对于所有可见的单元格,该单元格为零。我已经使用 NSNotification 将数据从一种方法发送到 ViewController.m ,而我希望通过 indexPath 将数据更新到单元格的 Reiever 方法。但所有单元格都是 nil 并且不能更新。
这里是我的代码 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 中的数据的解决方案示例
【问题讨论】:
-
将
dequeueReusableCellWithIdentifier切换为dequeueReusableCellWithIdentifier:forIndexPath: -
你有多少节?
-
尝试在'viewWillAppear'方法上重新加载tableView
-
@BlackRider 我已更改为
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];但仍有 cell==nil -
@anhtu 你的意思是我有多少行,如果我有 20 个部分
标签: ios objective-c uitableview