自定义UITableViewCell不会出现在第二次加载时

时间:2012-05-19 15:23:43

标签: objective-c ios uitableview

我在自己的视图单元格中有自定义的uitableview和自定义的uitableviewcell。单元格在第二次加载时不显示。也就是说,当我从这个视图转到另一个视图并回到这个视图时。

这是我在视图中加载表格的方式。

CropTableViewController *cropTblViewCon = [[CropTableViewController alloc]init];
 self.cropTableViewController = cropTblViewCon;
cropTableViewController.view.frame = cropTblView.frame;
[cropTblView removeFromSuperview];  
[self.view addSubview:cropTableViewController.view];
self.cropTblView = cropTableViewController.tableView;

这是我进入另一种观点的方式

AppDelegate_iPhone *appDelegate = [[UIApplication sharedApplication] delegate];
AddPestReportStepOne *report = [[AddPestReportStepOne alloc]init];
[appDelegate.navCon pushViewController:report animated: YES];   
[report release];   

这是我在自定义表视图中加载表格单元格的方法。

static NSString *CellIdentifier = @"CustomCropCell";    
CustomCropCell *cell = (CustomCropCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
tableView.scrollEnabled = NO;
//  tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    NSArray *topLevelObjects = [[NSBundle mainBundle] 
                              loadNibNamed:@"CustomCropCell" owner:nil options:nil];

    for (id currentObject in topLevelObjects){

        if ([currentObject isKindOfClass:[UITableViewCell class]]) {
            cell = (CustomCropCell *) currentObject;
            break;
        }
    }
}
//if (counter<6 ) {

CropEntity *crop = [[CropEntity alloc] init];
crop=[ cropList objectAtIndex:counter];

NSString *imgStr = [NSString stringWithFormat:@"%@%@%@",domainName,cropImagePath, [crop cropImage]];    
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgStr]]];
CGSize size = CGSizeMake(80, 80);
[cell setBackgroundColor: [UIColor clearColor]];
image = [Utilities scale:image toSize:size ];
[cell.columnOne setTag: [[crop cropId] integerValue] ];
[cell.columnOne setTitle:[crop cropName] forState:UIControlStateNormal];
[cell.columnOne setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.columnOne setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -image.size.width, -75.0, 0.0)]; // Left inset is the negative of image width. counter = counter+ 1;
[cell.columnOne setImage:image forState:UIControlStateNormal];
[cell.columnOne setImageEdgeInsets:UIEdgeInsetsMake(-15.0, 0.0, 0.0, -cell.columnOne.titleLabel.bounds.size.width)]; // Right inset is the negative of text bounds width.   
[cell.columnOne setBackgroundColor: [UIColor clearColor]];
counter = counter + 1;

似乎没有删除行。我的cellforrowat索引路径不断增加...

1 个答案:

答案 0 :(得分:0)

好的,我假设您要将一个UITableView添加到ViewController。以下是此步骤:

(1)您已经创建了一个UIViewController,无论是代码还是接口构建器

(2)创建一个UITableView,并将其添加到你的UIViewController

UITableView *tableView = [[UITableView alloc] 
                          initWithFrame:CGRectMake(0, 0, 320, 440) 
                          style:UITableViewStylePlain];

// Set up the tableView delegate and datasource

[self.view addSubview:tableView];

[tableView release];

我确定你有理由将tableView添加到UIViewController(没有理由有额外的UITableViewController。如果不是只使用UITableViewController,并设置UITableView。

另外,请记住在哪里创建tableView。例如,如果您在init或viewDidLoad中创建它并使用UINavigationController,当您点击UINavigationController上的后退按钮时,ViewDidLoad或init不会被调用。自己尝试一下,放一个NSLog语句进行测试。

所以使用viewDidAppear或[tableView reloadData];

编辑:

考虑一下您创建UITableViewContoller的代码,然后从它的超级视图中删除它的UITableView(没有意义),然后将UITableViewControllers视图(UITableView)添加到self.view,这是我猜的UIViewController。当然,这不是你想做的事情。

我建议阅读Apple的View Programming指南,以便更好地理解UIViewControllers和Views。

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaViewsGuide/Introduction/Introduction.html