2012-03-12 101 views
-2

这是代码我有:的UITableView拍击滚动

- (id)init 
{ 
    self = [super initWithStyle:UITableViewStylePlain]; 
    if (self) { 
     //self.tableView.delegate = self; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIColor *wood = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wood_pattern.png"]]; 
    self.view.backgroundColor = wood; 
    self.tableView.separatorColor = [UIColor clearColor]; 
} 

#pragma mark - Table view data source 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 3; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.backgroundView.backgroundColor = [UIColor redColor]; 
    } 

    // Configure the cell... 

    cell.textLabel.text = @"Test"; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

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

当我回来向下滚动,然后由这是以前在屏幕上,应用程序崩溃的细胞。任何想法为什么?

在侧面注释中,背景颜色设置为红色,尽管我可以看到文字,但看不到背景颜色。

+3

当开发人员看到崩溃时,我们通常会查找Crash日志所说的内容。如果没有崩溃,通过查看代码记录它浪费我们的时间。你能告诉我们碰撞日志吗? – 0x8badf00d 2012-03-12 02:34:51

+1

在if语句之外设置单元格颜色,您应该看到红色,但我需要查看错误以获取进一步帮助 – Vikings 2012-03-12 02:48:10

+0

我遇到了xcode不断出现问题的问题(非粗体nslogs,暗示最不可能完成我的代码,当我输入时跳来跳去,每当我尝试添加一个文件等等时,不要自己去选择目标框),现在它不会给我崩溃日志。 – Andrew 2012-03-12 02:49:55

回答

1

在XIB中正确绑定UITableView。并遵循以下代码。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tableView.separatorColor = [UIColor clearColor]; 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 3; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 
    cell.contentView.backgroundColor = [UIColor redColor]; 
    cell.textLabel.text = @"Test"; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    // Configure the cell. 

    return cell; 
}