iphone - 设置 UITableView 的样式

标签 iphone objective-c ios uitableview

我有一个在另一个类中调用的 UITableView 类

 MyPocketTableView * myPocketTableView = [[MyPocketTableView alloc]initWithFrame:CGRectMake(85, 153, 235, 250) style:UITableViewStyleGrouped]; 

但是,UITableViewClass 中的init 方法不允许我这样做。

@implementation MyPocketTableView

- (id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame];
    self = [super initWithStyle:style]

    if (self) {

        //[self setFrame:CGRectMake(85, 153, 235, 250)];
        self.dataSource = self;
        self.delegate = self;

    }
    return self;
}

我不能在 init 方法定义上添加 style : (UITableViewStyle) 样式,我也不能做 self = [super initWithStyle:style] 因为它提示说

No visible @interface for 'UITableView' declares the selector initWithStyle.

创建类时默认的 init 方法是 - (id) initWithFrame:(CGRect)frame。 MyPocketTableView 是 UITableView 的子类。

  @interface MyPocketTableView : UITableView <UITableViewDataSource,UITableViewDelegate>

最佳答案

需要将接口(interface)设置为UITableViewController,然后使用initWithStyle:

此外,在执行以下操作时,无需设置 UITableView 协议(protocol)委托(delegate)

@interface MyPocketTableView : UITableViewController

然后你可以调用:

MyPocketTableView * myPocketTableView = [[MyPocketTableView alloc] initWithStyle:UITableViewStyleGrouped];

以及以下内容:

- (id) initWithStyle:(UITableViewStyle)style 
{
    self = [super initWithStyle:style];
    if (self) {
        [self setFrame:CGRectMake(85, 153, 235, 250)];
    }
    return self;
}

您还可以使用以下(示例) 自定义方法:您正在调用的 key :

self = [super initWithStyle:UITableViewStyleGrouped];

- (id) initWithSomeCustomMessage:(NSString*)message
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        [self setFrame:CGRectMake(85, 153, 235, 250)];
        self.customMessage = message;
    }
    return self;
}

关于iphone - 设置 UITableView 的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12887621/

相关文章:

ios - 显示来自服务器的更新数据的 watch 套件

ios - 在 iOS 中滤除蓝光

objective-c - 将简单的 Objective-C block 转换为 Swift 时遇到问题

iphone - 使用不同的 Bundle Identifiers 自动构建 iOS

iphone - 如何将 RKObjectMapping (Reskit) 转换为 json 字符串?

iphone - 在 cocos2d 层顶部的 uitableview 中制作自定义 uitableviewcell

iphone - Objective-C 中的纺织品解析?

ios - objective-c 停止扫描 AV Foundation

ios - 我试图做一个简单的循环,每次运行它都会崩溃

iphone - ios在字符串中插入空格的方法