Reputation: 7930
I create a UITableView programmatically and add it to a UIView:
edit1
- (void)viewDidLoad
{
[super viewDidLoad];
previewTableView = [[UITableView
alloc]initWithFrame:CGRectMake(self.previewView.frame.origin.x,
self.previewView.frame.origin.y,
self.previewView.frame.size.width,
self.previewView.frame.size.height
)];
previewTableView.delegate = self;
previewTableView.dataSource = self;
previewTableView.tag = 1;
[self.previewView addSubview:previewTableView];
}
The previewView is created with XIB.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{
NSLog(@"Height for row %d",tableView.tag);
if (tableView.tag == 1)
{
CGFloat height=((UIImage*)[images objectAtIndex:indexPath.row]).size.height;
NSLog(@"section: %i, image height: %f",indexPath.section,height);
return height;
}
return 80;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == 1)
return [images count];
return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"tag = %d",tableView.tag);
if (tableView.tag == 1)
{
NSLog(@"indexpath images %d %d",indexPath.row,[images count]);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if ([images count] > [indexPath row])
return cell;
[cell.contentView addSubview:[images objectAtIndex:[indexPath row]]];
[cell.contentView sizeToFit];
return cell;
}
The table is not shown (no scrolling, no rows...blank view). And in fact, the NSLog is not getting called. (images contain 2 items). Any syggestion?
Upvotes: 0
Views: 81
Reputation: 6918
Instead of
previewTableView = [[UITableView alloc] init];
use:
previewTableView = [[UITableView alloc] initWithFrame:CGRectMake(101, 45, 100, 416)];
Your numberOfRows must return more than 0.
Upvotes: 2
Reputation: 652
you have to specify the frame for the tableview while creating programatically. ex:
tab2 = [[UITableView alloc]initWithFrame:CGRectMake(0, 540, 768, 600)];
if you want table to occupy full screen.
tab = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
Upvotes: 3