UITableview - 无法重新加载数据

Posted

技术标签:

【中文标题】UITableview - 无法重新加载数据【英文标题】:UITableview - Can't Reload data 【发布时间】:2013-10-07 10:48:07 【问题描述】:

我是 Obj-c 编程的菜鸟。我正在制作一个由 ViewController 组成的 iPad 应用程序,它向我显示 2 个文本框,上方的一个按钮,只是为了求和。

在同一个 Viewcontroller 中,我有一个 TableView 和一个 Button。这个 Tableview 最初向我展示了一个元素,但我想刷新这个列表只需单击按钮。

在按钮上链接的刷新列表(触摸事件)的功能称为“listaPDF”。

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface VPViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
    IBOutlet UITextField *txtPrimoAddendo ;
    IBOutlet UITextField *txtSecondoAddendo ;
    IBOutlet UILabel *lblTotale ;
    IBOutlet UITableView *tabella;
    NSMutableArray *lista;
   

@property (nonatomic, retain) IBOutlet UITextField *txtPrimoAddendo;
@property (nonatomic, retain) IBOutlet UITextField *txtSecondoAddendo;
@property (nonatomic, retain) IBOutlet UILabel *lblTotale;
@property (nonatomic, retain) IBOutlet UITableView *tabella;
@property (nonatomic, retain) NSMutableArray *lista;

-(IBAction)somma ;
-(IBAction)listaPDF;

@end

Viewcontroller.m

    #import "VPViewController.h"

    @interface VPViewController ()

    @end

    @implementation VPViewController

    -(void)somma 

    int x = [[txtPrimoAddendo text] intValue];
    int y = [[txtSecondoAddendo text] intValue];
    int somma = x + y ;

    NSString *totale = [NSString stringWithFormat:@"La somma fa : %d", somma];

    [lblTotale setText:totale];
    [lblTotale setHidden:FALSE];


    
    - (void)listaPDF 

    int contatore = 1;

    NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
    for (int i = 0; i < dirFiles.count ; i++) 

        NSString *files = dirFiles[i];
        int indice = files.length - 4 ;
        NSString *extension = [files substringFromIndex:indice];

        if([extension  isEqual: @".pdf"]) 

            NSLog(@"********* File PDF : %@", files);
            [lista insertObject:files atIndex:contatore];
            contatore++;
        
    
    [tabella reloadData];

    

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    
        return [lista count];
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
    static NSString *simpleTableIdentifier = @"CellID";

    UITableViewCell *cell = [tabella dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    
    cell.imageView.image = [UIImage imageNamed:@"pdf.png"];
    cell.textLabel.text = [lista objectAtIndex:indexPath.row];
    return cell;
    


    - (void)viewDidLoad
    
        tabella.delegate = self;
        tabella.dataSource = self;

        tabella = [[ UITableView alloc] init];
        lista = [[NSMutableArray alloc] init];

        [lista insertObject:@"FIRST ELEMENT" atIndex:0];


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

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

-(BOOL)textFieldShouldReturn:(UITextField *)textField
    [textField resignFirstResponder];
    return YES;


@synthesize lblTotale;
@synthesize txtPrimoAddendo;
@synthesize txtSecondoAddendo;
@synthesize tabella;
@synthesize lista;


@end

谢谢

【问题讨论】:

【参考方案1】:

我已经按原样尝试了您的代码。

为什么在 viewDidLoad 中初始化 UITableView 对象。如果您在 Xib 文件上添加,则不需要分配。如果您以编程方式创建 UITableView,那么它是必需的。只需注释/删除将调用初始化行和 tableView 的委托方法。

并且在“ListaPDF”函数中,没有任何项目被添加到数组“lista”中,这就是 UITableView 中没有新条目可见的原因。

【讨论】:

【参考方案2】:

NSMutableArray 索引从 0 开始 - 而不是 1

int contatore = 1;

...

[lista insertObject:files atIndex:contatore];
contatore++;

使用addObject 将对象添加到NSMutableArray

[lista addObject:files];

删除所有实例变量,因为:

 @property (nonatomic, retain) NSMutableArray *lista;

创建一个名为 _lista 的实例变量 - 而不是 lista

你有相当多的代码倒退:

tabella.delegate = self;
tabella.dataSource = self;

tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];

您正在尝试在分配表之前设置delegatedatasource

应该是:

tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];

tabella.delegate = self;
tabella.dataSource = self;

不过 UITableView 已经添加到 Interface Builder 中,对吧?

这意味着它已经为你创建好了,你不应该在这里分配表。

另外:在 Interface Builder 中,您可以右键单击+拖动(或 ctrl+leftclick+drag)将您的UITableView 连接到您的UIViewController,作为其delegatedatasource

【讨论】:

谢谢!有了你的回答和 Shabah 的回答,它就开始起作用了!

以上是关于UITableview - 无法重新加载数据的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 无法从 CustomCell 重新加载数据

无法重新加载 UITableView

更改单元格中的数据后,UITableView 中的自定义单元格将无法正确重新加载

解析查询后在哪里重新加载 UITableView 数据

刷新 UITableView tableFooterView 而不重新加载整个表数据

UITableView 在 iOS 7.1 上无法正确重新加载