2

我用 IB 创建了 uitableview。我设计了自己的 uitableviewcell(它是 uiatbleviewcell 的子类,有自己的 xib 文件)。如何将其添加到我的 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

4

3 回答 3

1

u need to change the default height of each cell using method

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

hope it helps. happy coding :)

于 2012-09-03T13:26:53.723 回答
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;
}
于 2012-09-03T13:47:33.803 回答
0

You dont have to add it like this on your xib file. Acc to what you have done you have added a UITableViewCell on another UITableView. Do you really want to create a custom cell ?? You can simply add any data into the UITableView in the

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath Then add the cell's accordingly like UILabels , UIImageViews etc .

 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;

Something like the above.

于 2012-09-03T13:27:36.243 回答