How to give space between two cells in tableview?

前端 未结 26 1492
谎友^
谎友^ 2020-11-29 00:11

I want a space between two cell in table view,

I want cell like this,

\"enter<

26条回答
  •  暖寄归人
    2020-11-29 00:56

    For people that looking for alternative way of showing gaps between cells without using sections, you may want to show alternate colours and height like below. Use clearColor for the spacing.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
        if (indexPath.row % 2 == 1)
        {
            static NSString *CellIdentifier = @"cellID1";
            UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                reuseIdentifier:CellIdentifier];
            }
    
            cell.backgroundColor = [UIColor colorWhite];
    
            return cell;
    
        } else {
    
            static NSString *CellIdentifier2 = @"cellID2";
            UITableViewCell *cell2 = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
            if (cell2 == nil) {
                cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                reuseIdentifier:CellIdentifier2];
            }
            cell2.backgroundColor = [UIColor clearColor];
    
            return cell2;
        }
    
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row % 2 == 1) {
            return 40.0;
        } else {
            return 2.0;
        }
    }
    

提交回复
热议问题