Search code examples
iosobjective-cuitableviewshadow

UITableView Shadow not coming properly (mis aligned)


Am trying to add bottom shadow to UITableViewCell and its coming but misaligned. This is my code and i have uploaded my screenshot also

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}


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

    if(indexPath.row==0){
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell1"]];
    // Configure the cell...
    }else if(indexPath.row==1){
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell2"]];
    }else if(indexPath.row==2){
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell3"]];

    }else if(indexPath.row==3){
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell4"]];

    }else {
    cell.contentView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell5"]];

    }
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.layer.shadowOpacity = 0.5;
    cell.layer.shadowOffset = CGSizeMake(0, 1);
    return cell;
}

Screenshot

enter image description here How to align that shadow at bottom properly?


Solution

  • Please update your code with this

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
    av.backgroundColor = [UIColor clearColor];
    av.opaque = NO;
    
    if(indexPath.row==0){
    
    // Configure the cell...
        av.image = [UIImage imageNamed:@"cell1"];
        cell.backgroundView = av;
    }else if(indexPath.row==1){
        av.image = [UIImage imageNamed:@"cell2"];
        cell.backgroundView = av;
    }else if(indexPath.row==2){
        av.image = [UIImage imageNamed:@"cell3"];
        cell.backgroundView = av;
    }else if(indexPath.row==3){
        av.image = [UIImage imageNamed:@"cell4"];
        cell.backgroundView = av;
    }else {
        av.image = [UIImage imageNamed:@"cell5"];
        cell.backgroundView = av;
    }
    
    return cell;
    

    }