Thêm các nút vào Ô Chế độ xem Bảng tùy chỉnh trong Lớp Bảng Xem tùy chỉnh (Adding buttons to custom Table View Cell under custom TableView Class)


問題描述

Thêm các nút vào Ô Chế độ xem Bảng tùy chỉnh trong Lớp Bảng Xem tùy chỉnh (Adding buttons to custom Table View Cell under custom TableView Class)

Edit: See my answer for a functioning app that somehow implemented what I was trying to do. 

I've checked around for this and followed every available tutorial ‑ this all seems pretty straightforward but my Storyboard & inspector simply will now allow me to do the following: 

‑‑ Add buttons to custom UITableViewButtons (using custom class 'Song Cell')

Every time I try to do so, it puts the button on a view which is above the table view. I've tried setting the cells to dynamic, static, basic, and every other toggle switch I could find. 

I think its because I have a slightly awkward settup in terms of views, so I tried to set my TableView to a custom class as well. However, its not showing up in Storyboard's Class Inspector. Here is what I did, to set this Table View to a custom class, so no avail:  

‑‑ Create custom class inheriting from UITableViewController, called SongTableViewController

‑‑ Set, in Storyboard, a table view controller's class to SongTableViewController

See this hierarchy:

// edit ‑ Apparently I do not have 10 rep to post pics, so I'll just draw it myself:

▼ Voting View Controller ‑ Current Songs
   ▼ View
     ▼ Table View     // This is where I would like the custom class, SongTableViewController
       > Song Cell     
       > Song Cell    //  These cells are where I would like to add the custom buttons
       > Song Cell
       > Constraints
     > Label ‑ 00:00
     > Label ‑ Voting will reset in:
    Navigation Item ‑ Current Songs
First Responder
Exit

When I select the Table View and go to the inspector to change its class, there is no option except for UITableView. Trying to hardcode this and hitting 'return' also does nothing.

Is my inability to add buttons to these cells due to the structure of my views? Or something else?


參考解法

方法 1:

Maybe you should create a xib with your custom cell. For exemple, the class "CustomeCell" with the xib "CustomeCell.xib".

You put some objects on your cell via xib file and in your class with your UITableView, do something like this:

‑ (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }
    // configure cell 

    return cell;
}

Don't forget to link you datasource and delegate for your TableView in the xib file AND add the delegates in your UITableView class :)

Storyboards are usefull but sometimes, the good way is to use xib files :)

EDIT: You can read this tutorial, it's a very good example how to manage custom cell / tableview with xib files: http://www.appcoda.com/customize‑table‑view‑cells‑for‑uitableview/

Hope it help you :)

方法 2:

You probably don't want to change the class of the table view. It sounds like what you really want to do is change the class of one (or more) of the cell prototypes. In the storyboard select one of the cells and change its class to your song cell class.

方法 3:

Here is a description of what was going on, and an example of how to impliment this: 

  

You have one UIViewController subclass, and add   the table view to it by dragging and dropping in the storyboard.

     

You then have a bit of extra work to do to fill the gap between a   table view controller and a plain view controller ‑ declare that you   conform to the datasource and delegate protocols, create an outlet for   the table view, connect the outlet to the table view and connect the   table view's delegate and datasource outlets to your VC.

     

Implement the three datasource methods (number of sections, number of   rows and cellForRow...) and you're done.

Link to Prototype

(by CDDLapinouHairOfTheDogCDD)

參考文件

  1. Adding buttons to custom Table View Cell under custom TableView Class (CC BY‑SA 3.0/4.0)

#storyboard #uitableview






相關問題

如何在 XAML 中為資源設置動畫? (How to animate a resource in XAML?)

使用情節提要設置 GLKView 的初始大小 (Set an initial size of GLKView with storyboard)

在情節提要中為“平鋪”創建更大的反面視圖 (Creating a larger flip-side view for 'tile' in storyboard)

XNA/XAML 鼠標懸停時動畫邊框顏色 (XNA/XAML Animate border color on mouseover)

帶有自定義按鈕的故事板 (Storyboard with Custom Buttons)

添加子視圖控制器並更改其標籤 (Adding a child view controller and changing its label)

Thêm các nút vào Ô Chế độ xem Bảng tùy chỉnh trong Lớp Bảng Xem tùy chỉnh (Adding buttons to custom Table View Cell under custom TableView Class)

ios在表格視圖單元格中視覺調整圖像大小 (ios visually resize image in a table view cell)

如何解決“在類型'ControlTemplate'中找不到可附加屬性'觸發器'”? (How to resolve "The attachable property 'Triggers' was not found in type 'ControlTemplate'"?)

Xcode 7.3、Swift 2.2 故事板極慢 (Xcode 7.3, Swift 2.2 Storyboards Extremely Slow)

Xcode Storyboard 警告未提供上下文:不支持的約束屬性配置 (Xcode Storyboard Warning not providing context: Unsupported Configuration of constraint attributes)

推送視圖控制器在 Swift 中不起作用 (Push view controller not working in Swift)







留言討論