ios - Swift 中的 UITableView 子类

标签 ios swift

我正在尝试在 Swift 中创建 UITableView 的子类。

var tableScroll = HorizontalScrollTableView(frame: CGRectMake(15, 15, cell.contentView.frame.width - 30, cell.contentView.frame.height - 30))
cell.contentView.addSubview(tableScroll)

我按以下方式添加表格,然后我有一个带有

的 Horizo​​ntalScrollTableView swift 文件
import UIKit

class HorizontalScrollTableView : UITableView {

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

但是,该表显示为普通默认表,并且 Horizo​​ntalScrollTableView 中的函数不会覆盖默认表。

最佳答案

您可能希望覆盖 numberOfSections 而不是 numberOfSectionsInTableView:,这是 UITableViewDataSource 协议(protocol)上的一个方法。但是,我认为创建您自己的 UITableViewController 子类或符合 UITableViewDataSource 的类并将其设置为 TableView 的委托(delegate)而不是 TableView 本身的委托(delegate)更为明智。子类化 UIView 及其后代通常保留用于自定义特定于 View 的功能和属性,例如添加自定义绘图或自定义触摸处理。

关于ios - Swift 中的 UITableView 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30793385/

相关文章:

ios - 如何阻止 TableViewCell 重复图像

swift - Watson VisualRecognition 无法对图像进行分类,报错 400(使用 swift)

swift - “Collection”要求类型 'MyCollectionClass.Element' 和 'Slice< MyCollectionClass>' 等效

ios - 为什么 Xcode 中 Storyboard 的背景颜色没有改变?总是相同的灰色吗?

ios - 外围设备服务在所有 BLE Scanner iOS 应用程序中显示为零,但在安卓代码中不显示

ios - 显示提醒(警报),例如自定义警报,并在 IOS 中对该警报执行用户操作

ios - 检测在 tableview : Swift Best Practices 中按下的 uibutton

android - soomla : cocos2dx : Profile -> "ProviderNotFoundException" (cocos2d3. x)

iphone - 如何从数据源中删除 NSManagedObject 实例?

ios - 根据屏幕大小调整按钮的宽度和高度