2012-09-03 33 views
2

我有用IB创建的uitableview。我设计了我自己的uitableviewcell(它是uiatbleviewcell的子类,并有自己的xib文件)。如何将它添加到我的uitableview?如何将自定义的uitableviewcell添加到用ib创建的uitableview?

我试图

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

static NSString *CellIdentifier = @"myCell"; 
myCell *cell = (myCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"myCell" owner:self options:nil]; 
    cell = [xib objectAtIndex:0]; 
} 
// Configure the cell... 
cell.title.text = @"tableCell"; 
cell.preview.text = @"preview"; 
cell.postedTime.text = @"right now"; 


return cell; 
} 

它的工作原理不正确。我不能在这里包含图片,因为我的代表是10(最少需要11才能上传图片)。所以这里的链接显示我的坏结果。这就是我想要的:http://img708.imageshack.us/img708/5082/20120903153930.png

这是我有: http://img836.imageshack.us/img836/5844/20120903154405.png

+0

单击单元格....打开Identity Inspector并将自定义单元格的类更改为“myCell”。然后附上奥特莱斯。也许它会帮助你... –

回答

0

你不必在你的xib文件中像这样添加它。根据你所做的,你在另一个UITableView上增加了一个UITableViewCell。你真的想创建一个自定义单元?您可以简单地将任何数据添加到UITableView中,然后像UILabels,UIImageViews等那样添加单元的相应内容。

cell = [tableView dequeueReusableCellWithIdentifier:nil]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil ]; 
    }   
    UIButton *Imagebutton = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 30, 30)]; 
    UIImage *img = [UIImage imageNamed:@"Lock Unlock.png"]; 
    [Imagebutton setImage:img forState:UIControlStateNormal]; 
    [Imagebutton addTarget:self action:@selector(aMethod1:) forControlEvents:UIControlEventTouchDown]; 
    [cell.contentView addSubview:Imagebutton]; 
    UILabel *MealsforLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 140, 30)]; 
    MealsforLabel.backgroundColor = [UIColor clearColor]; 
    [cell.contentView addSubview:MealsforLabel]; 
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.textLabel.textColor=[UIColor whiteColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    return cell; 

就像上面那样。

+0

你的意思是以编程方式自定义单元格吗?设置标签坐标等? – pash3r

+0

我的意思是综合图像视图,标签并为它们创建一个对象,并使用像cell.imageview,cell.label等对象以编程方式指定它们。 – IronManGill

+0

我怎么能为他们设置正确的位置? – pash3r

1

u需要使用方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 62.0f; // enter value if u know or test 
} 

希望它有助于改变每个单元的默认高度。快乐编码:)

+0

让我知道它是否有帮助,如果是的话,请接受答案,以便将来帮助他人。谢谢 –

+0

它部分帮助。但是在IB中不一样。它只显示标签和高度是正确的,但bg颜色是白色,而不是我设置的颜色:( – pash3r

+0

背景颜色没有设置,并且是否使用任何代表?如果是,您需要设置代理 –

1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if(btnTag == 1) 
    return [array1 count]; 

if(btnTag == 2) 
    return [array2 count]; 
return YES; 

} 


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


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

static NSString *CellIdentifier = @"Cell"; 

CustomCell *cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

if(btnTag == 1) 
    { 
    cell.teamLabel.text = @"i am 'A'cell"; 
    } 
if(btnTag == 2) 
    { 
    cell.teamLabel.text = @"i am 'B'cell"; 
    } 
return cell; 
} 
相关问题