0

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 :)

Melanie Journe
  • 1,249
  • 5
  • 16
  • 36
  • Are you sure about the content of the data array? self.interestsCategory[indexPath.row].name, whats the output of the printf? – Ulysses Mar 07 '16 at 09:38
  • I try that in `cellForRowAtIndexPath` and it marks overtime Optional("Real Estate") but I also get cellForRowAtIndexPath : 4 and I'm sure I don't add the same element 12 times – Melanie Journe Mar 07 '16 at 09:47
  • Initialise your array in the viewDidLoad, and see if this make any change. – Ulysses Mar 07 '16 at 09:50
  • I edited my post, for the repetition of the same item it was my fault in the `InterestCategory` class i left a mistake. But do you have any idea for the white space ? and the separator ? – Melanie Journe Mar 07 '16 at 09:53
  • it is not the best thing to use fixed frames when we have a lot of screen sizes today, and the magical powerful autolayout, also a nice thing is the interface builder to help you creating things. you can try to refer to this and see if it helps with the white space http://stackoverflow.com/questions/27547149/scrollview-adds-space-at-the-top-of-subview-xcode-6-swift/27547371#27547371 – sken3r.MI Mar 07 '16 at 09:54
  • Or if you want to set it from code in `didLoad` function set `self.automaticallyAdjustsScrollViewInsets = false; – sken3r.MI Mar 07 '16 at 10:02

2 Answers2

0

Issue1

Please set

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

Issue 2 separator inset

-(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];
    }
}
techloverr
  • 2,597
  • 1
  • 16
  • 28
  • 1. the Delegate and data source are set, because you can see the count and the data in the table view 2. your answer is in Obj-C and this is not necesary, a simple `self.automaticallyAdjustsScrollViewInsets = false` should fix this – sken3r.MI Mar 07 '16 at 10:08
  • @sken3r.MI 1. you can see in code, she is initializing tableView but not setting 2. this is not scroll issue it is separator issue so you can't fix via `self.automaticallyAdjustsScrollViewInsets = false` – techloverr Mar 07 '16 at 10:10
  • @sken3r.MI this all thousands of friends of mine are also feeling bad because you were not there in at this solution http://stackoverflow.com/a/25877725/2963912 – techloverr Mar 07 '16 at 10:18
  • @sken3r.MI you should also help us via giving your iOS to us so that we can use `self.automaticallyAdjustsScrollViewInsets = false` – techloverr Mar 07 '16 at 10:21
  • I'm using a UITableViewController so I don't need to set the delegate & data source. For the white rect I end up setting `self.tableView.backgroundColor = UIColor.clearColor();` but I can still see a space, which is not that problematic with the background color. – Melanie Journe Mar 07 '16 at 11:09
  • ma'am then why are you initializing tableview here `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)` – techloverr Mar 07 '16 at 11:13
0

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.

Melanie Journe
  • 1,249
  • 5
  • 16
  • 36