当前位置:编程学习 > wap >>

为什么调用“[tableView reloadData];”不起作用

请教各位高手,还是关于tableview刷新的问题。

我在.h里面,声明了tableview以及相关的数据源:
#import <UIKit/UIKit.h>

@interface CubeTimerViewController : UIViewController
    <UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet    UILabel        *statusText;
    IBOutlet    UITableView *tableView;
    NSMutableArray *score;
    NSMutableArray *titles;    
}
@property (retain, nonatomic) UILabel *statusText;
@property (retain, nonatomic) UITableView *tableView;
@property (retain, nonatomic) NSArray *score;
@property (retain, nonatomic) NSArray *titles;

- (IBAction)touchDown:(id)sender;
- (IBAction)touchUp:(id)sender;
- (void)updateLabel;

@end

然后在.m文件里面的touchDown方法里修改了数据源的值,并调用[tableView reloadData]刷新。
但是表格里的数据并没有变化。为了显示出是否被调用了,我在其内部声明了一个静态整数ii,每次调用时ii++,可是运行时触摸屏幕按钮,却ii的值却只是显示4。请问这是怎么回事呢?谢谢了。

下面是m文件:
#import "CubeTimerViewController.h"

@implementation CubeTimerViewController
@synthesize statusText;
@synthesize tableView;
@synthesize titles;
@synthesize score;

- (void)viewDidLoad {
    [statusText setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:64.0]];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"Best", @"Worst", @"Average1", @"Average2", nil];
    self.titles = array;
    array = [[NSMutableArray alloc] initWithObjects:@"0", @"0", @"0", @"0", nil];
    self.score = array;
    [array release];
    
    tableView.delegate = self;
    tableView.dataSource = self;

    [super viewDidLoad];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (IBAction)touchDown:(id)sender
{
    float iii = 2.0;
    NSString* texttmp222 = [[NSString alloc] initWithFormat:@"%.3f", iii];
    [score replaceObjectAtIndex:0 withObject:texttmp222];
    //statusText.text = [score objectAtIndex:0];
    [texttmp222 release];    
    [self.tableView reloadData];
}

- (IBAction)touchUp:(id)sender
{
}

- (void)updateLabel
{
}

- (void)dealloc {
    [statusText release];
    [titles release];
    [score release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [titles count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];

    static NSString *sectionsTableIdentifier = @"sectionsTableIdentifier";
    
    UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier: sectionsTableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
                                       reuseIdentifier: sectionsTableIdentifier] autorelease];
    }
    
    static int ii = 0;
    ii++;
    statusText.text =  [[NSString alloc] initWithFormat:@"[%d]", ii];//[score objectAtIndex:0];
    cell.text = [score objectAtIndex:section];
    cell.font = [UIFont boldSystemFontOfSize:20];
    return cell;
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if ([titles count] == 0)
        return @"";
    
    NSString *key = [titles objectAtIndex:section];
    
    return key;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView
    heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 29;
}

@end --------------------编程问答-------------------- 除了使用静态变量也可以设置断点查看。
我估计(UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这个协议方法可能没被调。



--------------------编程问答-------------------- 没有加点击事件吧 --------------------编程问答-------------------- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

在这里面设个断点吧,搞不好tableView是空的,不会进入这个协议。 --------------------编程问答-------------------- 感觉你的(IBAction)touchDown:(id)sender 可能没有被调用,你在Interface Builder里看看,又没有和事件关联好 --------------------编程问答-------------------- 我 的 reloadData也不好用 我 不用 xib 那么 tableView怎么 样才能 不为 空 并 代理此协议 --------------------编程问答-------------------- table = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 45.0f,320.0f,436.0f) style:UITableViewStylePlain];
    table.backgroundColor = [UIColor redColor];

table.delegate = self; 
table.dataSource = self; 

[self.view addSubview:table];
这样就解决啦 table 就不空啦    UITableView * table --------------------编程问答-------------------- 学习~~~ --------------------编程问答-------------------- 问题分析,如果tableview没有显示任何数据那就是数据源设置问题需要按照操作步骤1:
1.需要在IB里面指定委托和数据源。按着control键,拖动鼠标到File's Owner。选择Delegate 和DataSource,即可,或者在ViewLoad中设置:tableview的delegate和dataSource为self即可。
2.如果显示tableview显示数据,那就是touchDown没有被关联上,需要在ib中重新关联对应的动作.
3.有时候所有关联动作都操作,但对应xib文件没有被复制到模拟器下,需要clean target一下,曾遇到这中情况。
4.如果楼上要处理滑动效果,需要在自定义cell中捕捉touch事件。 --------------------编程问答-------------------- --------------------编程问答--------------------
引用 8 楼 wfuke2000 的回复:
问题分析,如果tableview没有显示任何数据那就是数据源设置问题需要按照操作步骤1:
1.需要在IB里面指定委托和数据源。按着control键,拖动鼠标到File's Owner。选择Delegate 和DataSource,即可,或者在ViewLoad中设置:tableview的delegate和dataSource为self即可。
2.如果显示tableview显示数据,那就是tou……


顶 --------------------编程问答-------------------- 我一直都在用啊,没有出现什么问题。 --------------------编程问答-------------------- --------------------编程问答-------------------- ib文件的有没有连接?
没数据可能
(UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这个没有调用。

如果第一次有数据,但刷新不了,看看是不是 reload的是不是你连接的那个 tableView! --------------------编程问答-------------------- 前面加上数据源的数据。 --------------------编程问答-------------------- 把 [self.tableView reloadData];
  这句 写在 -(void)viewWillAppear:(BOOL)animated:{}里面试试 --------------------编程问答-------------------- 清空数据源,然后重新加载数据放到array中,然后在reload试试~ --------------------编程问答-------------------- 在cellForRowAtIndexPath里打个日志,看看到底调进去了没 --------------------编程问答-------------------- 因为你这样写了@interface CubeTimerViewController : UIViewController
至于这样写没有,我也不清楚,假如你写成@interface CubeTimerViewController : UITableViewController,肯定可以reload,但是原理又有点不一样了
补充:移动开发 ,  iPhone
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,