pash3r
pash3r

Reputation: 161

how to add custom uitableviewcell to uitableview created with ib?

I have uitableview created with IB. And I designed my own uitableviewcell (it is subclass of uiatbleviewcell and has its own xib file). How to add it to my uitableview??

I tried

- (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;
}

It works incorrect. I can't include images here, because my rep is 10 (need min. 11 to upload images). So here's link to show my bad results. This is what i want: http://img708.imageshack.us/img708/5082/20120903153930.png

This is what I have: http://img836.imageshack.us/img836/5844/20120903154405.png

Upvotes: 2

Views: 2638

Answers (3)

IronManGill
IronManGill

Reputation: 7226

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.

Upvotes: 0

Prabhjot Singh Gogana
Prabhjot Singh Gogana

Reputation: 1408

- (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;
}

Upvotes: 1

Anshuk Garg
Anshuk Garg

Reputation: 1540

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 :)

Upvotes: 1

Related Questions