Melanie Journe Melanie Journe - 6 months ago 32
iOS Question

Swift - UITableView displaying weird white cell

I making a

tableView
of different item. The item have a name, and a picture.
Here is what I get :
enter image description here

And I want :
1 - to delete the white space which appears at the beginning and the end of the table view
2 - for the separator to be all the width

Here is my code :

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
}


If someone could help me, that would be great :)

Answer

These space came from this line :

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)

It should be

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 doesn't add space where style: .Grouped does.