Iphone 如何将自定义uitableviewcell添加到使用ib创建的uitableview?
我用IB创建了uitableview。我设计了自己的uitableviewcell(它是uiatbleviewcell的子类,有自己的xib文件)。如何将其添加到我的uitableview 我试过了Iphone 如何将自定义uitableviewcell添加到使用ib创建的uitableview?,iphone,uitableview,Iphone,Uitableview,我用IB创建了uitableview。我设计了自己的uitableviewcell(它是uiatbleviewcell的子类,有自己的xib文件)。如何将其添加到我的uitableview 我试过了 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"myCell"
- (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上传图像)。这里有一个链接显示我的坏结果。这就是我想要的:
这就是我所拥有的:
您需要使用方法更改每个单元格的默认高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 62.0f; // enter value if u know or test
}
希望能有帮助。快乐编码:)您不必像这样将其添加到xib文件中。根据您所做的,您在另一个
UITableView
上添加了一个UITableView单元格。是否确实要创建自定义单元格??您只需将任何数据添加到
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
然后添加相应的单元格,如UILabels、UIImageView等
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;
类似于上述内容。如果有帮助,请告诉我,如果有帮助,请接受答案,以便将来帮助他人。谢希特起了部分作用。但与IB中的不同。它仅显示标签和高度正确,但bg颜色为白色,而不是我设置的颜色:(背景色未设置。您是否使用任何委托?如果是,您需要将委托设置为以编程方式自定义单元格吗?设置标签的坐标等?我的意思是合成图像视图、标签并为它们创建一个对象,并使用该对象(如cell.imageview、cell.label等)以编程方式指定它们。谢谢请解释一下:Image=[[UIButton alloc]initWithFrame:CGRectMake(10,5,30,30)];,因为我有警告,图像是我在.h文件中声明的任何UIButton。你的警告是什么?首先在你的文件中声明你的UIButton*Imagebutton;或者再次查看我编辑的帖子:)抱歉,名称错误:请单击单元格…打开Identity Inspector并将自定义单元格的类别更改为“myCell”。然后连接插座。也许这会对您有所帮助。。。。
- (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;
}