0

I have a custom uitableview with custom uitableviewcell in my view cell. The cell doesn't show on second load. That is, when I go from this view to another and back to this view.

This is how I load my table in my view.

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

This is how I go another view

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

This is how I load my table cell in my custom table view.

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;

It seems that the rows are not removed. My cellforrowat indexpath keep increasing...

4

1 回答 1

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 guide,以更好地理解 UIViewControllers 和 Views。

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

于 2012-05-19T17:00:22.983 回答