0

I am a beginner in iOS development. I face a problem where I need little help.

I wrote a small program to learn custom UITableViewCell. It just works well at beginning; after that, when I slide the view, it changes size of the cell. I am confused about where I might be going wrong. The ContentView have only one view which is UIImageView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWHomePageCell"];
    if (cell == nil)
    {
        cell = [HWHomePageCell homePageCell];
    }
        cell.imageView.image = [UIImage imageNamed:@"XD"];

    return cell;
}

These are the pictures. Top two are good, but when I slide down, as you can see, the 3rd doesn't work well

enter image description here

Rohan Sanap
  • 2,773
  • 2
  • 21
  • 39
Evans.Y
  • 3
  • 3
  • You might want to add cell.imageView.contentMode = UIViewContentModeScaleAspectFill; Following link might help. http://stackoverflow.com/questions/6638623/uiviewcontentmodescaleaspectfill-not-clipping – Deepak Thakur May 31 '16 at 06:27

2 Answers2

1
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWHomePageCell"];

        cell = [HWHomePageCell homePageCell];

        cell.imageView.image = [UIImage imageNamed:@"XD"];

    return cell;
}

When you use custom cell then no need to check whether cell is nil or not. so please remove this condition and implement it.

yankit Patel
  • 331
  • 1
  • 12
  • Thanks A LOT! It works.But would you please tell me the reason? What happened to the method -- dequeueReusableCellWithIdentifier. Why when I comment out the "cell = [HWHomePageCell homePageCell];" in your code, the bug show again? – Evans.Y May 31 '16 at 08:37
  • When first time tableview load only visible cell is alloc in memory but remain cell is not load. "dequeueReusableCellWithIdentifier" Methods is unique identifier of each cell. when you scroll tableview every time this methods called. but it is not load cell every time. its identify cell with its Identifier. – yankit Patel Jun 02 '16 at 04:41
0
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25