Swift - UITableView显示奇怪的白色单元格

时间:2016-03-07 09:30:37

标签: ios swift uitableview

我制作了tableView个不同的项目。该项目有一个名称和一张图片。 这是我得到的: enter image description here

我想: 1 - 删除出现在表视图开头和结尾的空白区域 2 - 分隔符为全宽

这是我的代码:

CustomTableView

    // Controls
    var interestsCategory:[InterestCategory];


    // Load components when the view is loaded
    override func viewDidLoad() {
        super.viewDidLoad();

        //Hide Nav bar
        self.navigationController?.navigationBarHidden = true;

        // Table View
        self.tableView = UITableView(frame: CGRect(x: 0, y: 3.2 * self.view.frame.height / 13, width: self.view.frame.width ,height: 8.8 * self.view.frame.height / 13), style: .Grouped)
        //self.tableView.style = UITableViewStyle.Grouped;
        self.tableView.registerClass(ProximityInterestCategoryCell.self, forCellReuseIdentifier: "ProximityInterestCategoryCell");
        self.tableView.tableFooterView = UIView(frame: CGRectZero)
        self.tableView.tableHeaderView = UIView(frame: CGRectZero)
        //self.view.addSubview(tableView);

    }

init()
{
    self.interestsCategory = [];
    self.interestsCategory.append(InterestCategory(name:"FINANCE", image: UIImage(named: "business2.png")!));
    self.interestsCategory.append(InterestCategory(name:"SPORTS", image: UIImage(named: "sports2.png")!));
    self.interestsCategory.append(InterestCategory(name:"WEB",image: UIImage(named: "web2.png")!));
    self.interestsCategory.append(InterestCategory(name:"TOURISM",image: UIImage(named: "tourism2.png")!));
    self.interestsCategory.append(InterestCategory(name:"ENERGY",image: UIImage(named: "energy2.png")!));
    self.interestsCategory.append(InterestCategory(name:"TECHNOLOGY",image: UIImage(named: "technology2.png")!));
    self.interestsCategory.append(InterestCategory(name:"MUSIC",image: UIImage(named: "music2.png")!));
    self.interestsCategory.append(InterestCategory(name:"CHEMICAL",image: UIImage(named: "chemical2.png")!));
    self.interestsCategory.append(InterestCategory(name:"INSURANCE",image: UIImage(named: "insurance2.png")!));
    self.interestsCategory.append(InterestCategory(name:"HEALTH",image: UIImage(named: "health2.png")!));
    self.interestsCategory.append(InterestCategory(name:"FASHION",image: UIImage(named: "fashion2.png")!));
    self.interestsCategory.append(InterestCategory(name:"REAL ESTATE",image: UIImage(named: "realestate2.png")!));


    print(" number : \(self.interestsCategory.count)");

    super.init(nibName: nil, bundle: nil);
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    print("numberOfRowsInSection");

    return self.interestsCategory.count;
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    print("cellForRowAtIndexPath : \(indexPath.row)");
    print(self.interestsCategory[indexPath.row].name);
    let cellIdentifier = "ProximityInterestCategoryCell";
    let cell = self.tableView.dequeueReusableCellWithIdentifier( cellIdentifier, forIndexPath: indexPath) as! ProximityInterestCategoryCell;
    cell.nameLabel.text = self.interestsCategory[indexPath.row].name;
    cell.nameLabel.sizeToFit();
    cell.background.image = self.interestsCategory[indexPath.row].image;
    cell.background.clipsToBounds = true;
    return cell;

}

// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}

/*
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    print("heightForRowAtIndexPath");
    return 120;
}*/

CustomCell

 var background: UIImageView!
var nameLabel: UILabel!
var checkButton: UIButton!


override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    backgroundColor = UIColor.clearColor();
    selectionStyle = .None;

    self.background = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height:self.frame.height));
    self.background.alpha = 1;
    self.background.contentMode = UIViewContentMode.ScaleAspectFill;
    self.background.clipsToBounds = true;
    contentView.addSubview(background);

    self.nameLabel = UILabel(frame: CGRect(x: 50, y: 0, width: self.frame.width-50, height:self.frame.height));
    self.nameLabel.center.y = self.center.y;
    self.nameLabel.textColor = UIColor.whiteColor();
    self.nameLabel.backgroundColor = UIColor.clearColor();
    contentView.addSubview(nameLabel);

    self.checkButton = UIButton(frame: CGRect(x: 6 * self.frame.width/7.4, y: 0, width: 0.7 * self.frame.width/7.4, height: 0.8 * self.frame.height/13));
    self.checkButton.center.y = self.center.y;
    self.checkButton.setImage(UIImage(named: "RondNonCoche.png"), forState: UIControlState.Normal);
    self.checkButton.setImage(UIImage(named: "RondCoche.png"), forState: UIControlState.Selected);
    self.checkButton.clipsToBounds = true;
    contentView.addSubview(checkButton);
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func prepareForReuse() {
    super.prepareForReuse()

}

override func layoutSubviews() {
    super.layoutSubviews();
}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

如果有人可以帮助我,那就太棒了:)。

2 个答案:

答案 0 :(得分:0)

<强> Issue1

请设置

self.tableView.delegate = self
self.tableView.dataSource = self

问题2 分隔符插入

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
           [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

答案 1 :(得分:0)

这些空间来自这条线:

self.tableView = UITableView(frame: CGRect(x: 0, y: 3.2 * self.view.frame.height / 13, width: self.view.frame.width ,height: 8.8 * self.view.frame.height / 13), style: .Grouped)

应该是

self.tableView = UITableView(frame: CGRect(x: 0, y: 3.2 * self.view.frame.height / 13, width: self.view.frame.width ,height: 8.8 * self.view.frame.height / 13), style: .Plain)

style: .Plain不会在style: .Grouped的位置添加空格。