简体   繁体   中英

How do I embed a UITableView in a UIView?

I have a UIView in a custom view that I built using xib . I need to display a UITableView in the said view. At first I thought about placing a container and embedding a UITableViewController in it. Turns out I cannot place containers in a xib file, or atleast there's no way of doing it from the IB as it doesn't show up in views section at the lower right.

I can create a UITableView programmatically and add it as a subview of the view. It shows up as expected but I cannot seem to be able to add cells in it. I also tried creating a well behaving UITableViewController in association with a storyboard view, instantiate that controller as follows:

let storyboard = (UIStoryboard(name: "Main", bundle: nil)) let vc = storyboard.instantiateViewControllerWithIdentifier("tableViewController") as! TestTableViewController

and then tried accessing the UITableView 's outlet which was nil . Then I read somewhere that I should do a vc.loadView() because as the name suggests, it loads the view and my IBOutlet would not be nil . This worked. The outlet was on longer nil . But, when I add the table in the container view as a subview, it still shows no cells. There are only separator lines but no content. I've run out of ideas!

EDIT

I do not have any UITableViewCell implementations as the tables are static.

Good approach is to use UITableview inside your custom view:

if you are adding tableview programmatically then register your cell using Nib or UITableview subclass like

tableView.registerNib(UINib(nibName: "UITableViewCellSubclass", bundle: nil), forCellReuseIdentifier: "UITableViewCellSubclass") 

for if you are creating UITableviewCell using Xib.

tableView.registerClass(UITableViewCellSubclass.self, forCellReuseIdentifier: "UITableViewCellSubclass") // using code.
tableView.delegate = self
tableView.dataSource = self

and then use 2 required delegates.

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return tableView.dequeueReusableCellWithIdentifier("UITableViewCellSubclass", forIndexPath: indexPath) as! UITableViewCellSubclass
}

hope i answered your question.

Objective c You need delegates in your viewcontroller, if you have a viewcontroller, put delegate of table :

UIViewController UITableViewDelegate UITableViewDataSource

And you can use your functions

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [catagorry count];    //count number of row from counting array hear cataGorry is An Array
}



- (UITableViewCell *)tableView:(UITableView *)tableView
             cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *MyIdentifier = @"MyIdentifier";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier] autorelease];
        }

        // Here we use the provided setImageWithURL: method to load the web image
        // Ensure you use a placeholder image otherwise cells will be initialized with no image
        [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
                       placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell.textLabel.text = @"My Text";
        return cell;
    }

Swift :

delegate: UIViewController UITableViewDataSource UITableViewDelegate

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

Swift

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)

    let row = indexPath.row
    cell.textLabel?.text = swiftBlogs[row]

    return cell
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)

    let row = indexPath.row
    cell.textLabel?.text = swiftBlogs[row]

    return cell
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return swiftBlogs.count
}


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

Look more More inf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM