如何在 Xcode 中为我的 tableview 添加上边距?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30678391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:14:41  来源:igfitidea点击:

How can I add a top margin to my tableview in Xcode?

xcodeswiftpositiontableviewmargin

提问by Larry Navarro

I'm trying to add an image on the very top of the first cell of a table view. How can i make the table view have a top margin? Right now, the image is just overlapping the tableview. I am trying to just get the table view to come down a little bit. I do not want to make it smaller or something like that. I just want to add a button to the top of the tableview.

我正在尝试在表格视图的第一个单元格的最顶部添加一个图像。如何使表格视图具有上边距?现在,图像只是与 tableview 重叠。我试图让表格视图稍微下降一点。我不想让它变小或类似的东西。我只想在 tableview 的顶部添加一个按钮。

//
//  usersVC.swift
//  CaastRun
//
//  Created by Computer on 5/23/15.
//  Copyright (c) 2015 Caast. All rights reserved.
//

import UIKit

class usersVC: UIViewController, UITableViewDataSource, UITableViewDelegate {



    @IBOutlet weak var resultsTable: UITableView!

    var resultsNameArray = [String]()
    var resultsUserNameArray = [String]()
    var resultsImageFiles = [PFFile]()

    override func viewDidLoad() {
        super.viewDidLoad()
        let theWidth = view.frame.size.width
        let theHeight = view.frame.size.height
        resultsTable.frame = CGRectMake(0, 0, theWidth, theHeight)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(animated: Bool) {


        resultsNameArray.removeAll(keepCapacity: false)
        resultsUserNameArray.removeAll(keepCapacity: false)
        resultsImageFiles.removeAll(keepCapacity: false)

        var query = PFUser.query()

        query!.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)

        query!.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]?, error:NSError?) -> Void in

            if error == nil {

                for object in objects! {

                    self.resultsNameArray.append(object.objectForKey("profileName") as! String)
                    self.resultsImageFiles.append(object.objectForKey("photo") as! PFFile)
                    self.resultsUserNameArray.append(object.objectForKey("username") as! String)

                    self.resultsTable.reloadData()


                }

            }

        }

    }


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

        return resultsNameArray.count

    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 64

    }

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

        var cell:usersCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! usersCell

        cell.profileLbl.text = self.resultsNameArray[indexPath.row]
        cell.usernameLbl.text = self.resultsUserNameArray[indexPath.row]

        var query = PFQuery(className: "follow")

        query.whereKey("user", equalTo: PFUser.currentUser()!.username!)
        query.whereKey("userToFollow", equalTo: cell.usernameLbl.text!)

        query.countObjectsInBackgroundWithBlock {
            (count:Int32, error:NSError?) -> Void in

            if error == nil {

                if count == 0 {

                    cell.followBtn.setTitle("Follow", forState: UIControlState.Normal)

                } else {

                    cell.followBtn.setTitle("Following", forState: UIControlState.Normal)
                }

            }

        }


        self.resultsImageFiles[indexPath.row].getDataInBackgroundWithBlock {
            (imageData:NSData?, error:NSError?) -> Void in

            if error == nil {

                let image = UIImage(data: imageData!)
                cell.imgView.image = image

            }

        }

        return cell

    }

    @IBOutlet weak var searchText: UITextField!


    @IBAction func searchButton(sender: AnyObject) {
        resultsNameArray.removeAll(keepCapacity: false)

        resultsUserNameArray.removeAll(keepCapacity: false)

        resultsImageFiles.removeAll(keepCapacity: false)

        var query = PFUser.query()

        query!.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)

        query!.whereKey("profileName", containsString: self.searchText.text)

        query!.findObjectsInBackgroundWithBlock {

            (objects:[AnyObject]?, error:NSError?) -> Void in

            if error == nil {

                for object in objects! {

                    self.resultsNameArray.append(object.objectForKey("profileName") as! String)

                    self.resultsImageFiles.append(object.objectForKey("photo") as! PFFile)

                    self.resultsUserNameArray.append(object.objectForKey("username") as! String)

                    self.resultsTable.reloadData()

                }



            }

        }
    }


}

回答by Icaro

I am not sure if I understand your question, if you are trying to have a margin between your table and the view try this:

我不确定我是否理解你的问题,如果你想在你的桌子和视图之间留出一个边距,试试这个:

Swift 3

斯威夫特 3

self.tableView.contentInset = UIEdgeInsets(top: 20,left: 0,bottom: 0,right: 0)

Swift 2

斯威夫特 2

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);