0

I am trying to display name and images in UITableViewCell like the normal way

cell.textLabel.text=@"some name";
cell.imageView.image=[array objectAtIndex:indexPath.row];

and when i load the ViewController , I can see all images are coming correctly but when I tried to click on any of UITableviewCell image size is changing to big(please check the example screenshot).

Please help me to solve this problem.

Loading time it looks like

Loading time screen shot

When I click on the first cell ,the cell image becomes big like below image Cell image becomes large when i click on the cell

Please check my code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor=[UIColor clearColor];
        if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }
    cell.backgroundColor=[UIColor clearColor];
    if([userName count]>0){
    cell.textLabel.text=[userName objectAtIndex:indexPath.row];
        [cell.imageView setImageWithURL:[NSURL URLWithString:[[userList valueForKey:@"photo"] objectAtIndex:indexPath.row]]
                       placeholderImage:[UIImage imageNamed:@"avatar.png"]];
    }
    CGSize itemSize = CGSizeMake(40, 40);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    cell.imageView.layer.masksToBounds=YES;
    cell.imageView.layer.cornerRadius=9.0;


    return cell;
}

My Didselect method is like this

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *nib;

    if ([[MyDevice getMyDevice] isiPad])
    {
        nib = @"nibname_iPad";

    }
    else
    {
        nib = @"nibname";

    }


    ViewController *chat = [[ViewController alloc] initWithNibName:xibName bundle:[NSBundle mainBundle]];
    chat.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:chat animated:YES];


}
Naveen
  • 15
  • 5

2 Answers2

1

Try to use Custom Cells (Change the Style of the cell in the attributes inspector of the cell), its better and you can design it as the way you want. Create in the Story board the UILabels and the UIImage (with a specific width and height), finally in the attributes inspector of the UILabel and UIIamge set them with a specific Tag number, this number has to be unic for both of them. And access them like this:

In the method:

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

Insert this:

    UITableViewCell *customCell = [self.tableView dequeueReusableCellWithIdentifier:@"theNameOfTheCell"];

    UILabel *label;
    label = (UILabel *)[customCell viewWithTag:1];
    label.text = @"Some text to label with tag number 1";
    label = (UILabel *)[customCell viewWithTag:2];
    label.text = @"Some text to label with tag number 2";

    UIImageView *imageCell;
    imageCell = (UIImageView *)[customCell viewWithTag:3];
    CALayer * l = [imageCell layer];
    UIImage *cellImage = [UIImage imageNamed:@"theImageYouWant.png"];
    imageCell.image = cellImage;

Try with this and tell what append, this is the full Documentation: Official Documentation Table View Cells

Luis Mejías
  • 301
  • 2
  • 10
0

Add content mode:

[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];

Hope this helps... :)

Rashad
  • 11,057
  • 4
  • 45
  • 73