2017-01-18 5 views
0

私はこれを2日間苦労しています。私を助けてください。以下は配列データを迅速にカスタムUITableViewに渡すには?

class TopMenuTable: UITableView,UITableViewDelegate,UITableViewDataSource { 


    var topArray = [String]() 


    convenience init(frame: CGRect, style: UITableViewStyle, items : [String]) { 
     self.init(frame: frame, style:UITableViewStyle.Plain , items: [String]()) 

     self.scrollEnabled = false 
     self.dataSource = self 
     self.delegate = self 
     self.topArray = items 

     self.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") 
    } 


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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return topArray.count 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell 
     //let cell : TopMenuCell = tableView.dequeueReusableCellWithIdentifier("TopCell") as! TopMenuCell 
     cell.textLabel?.text = topArray[indexPath.row] 
     return cell 
    } 

のViewControllerにTopMenuTableを初期化するためのコードです:

func menuButtonClicked(sender : UIButton){ 

     let topMenu = TopMenuTable.init(frame: CGRectMake(topMenuBtn.frame.origin.x - 100, 0, 260, 0), style: .Plain, items: topMenuItems) 
     self.navigationController?.view.addSubview(topMenu) 

    } 


    EXE_BAD_ACCESS error is coming at this line. 
    convenience init(frame: CGRect, style: UITableViewStyle, items : [String]) 

同じことがobj cの細かい作業(以下のコードがある)ビューコントローラで

@implementation TopMenuTable 

    -(id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style 
    :(NSArray*)topArrayItems 
    :(NSDictionary *)topDict{ 

    if (self == [super initWithFrame:frame style:style]) { 
    [self setScrollEnabled:NO]; 
    topMenuItems = [topArrayItems mutableCopy]; 
    topMenuItemsDict = topDict; 
    self.dataSource = self; 
    self.delegate = self; 

    } 

    return self; 

} 

。私は以下のコードを初期化しています。

topMenu = [[TopMenuTable alloc]initWithFrame:CGRectMake(topMenuBtn.frame.origin.x - 100, 0, 260, 0) style:UITableViewStylePlain :topMenuItems :self.topMenuDictionary]; 
+0

は明らかに、あなたは自由ですあなたが何をしたいのですが、あなたはUITableViewをサブクラス化しますか?そして、あなたがしたとしても、そのクラスをIB/Storyboard –

+3

'self.navigationController?.view.addSubview(topMenu)'に使うことができます。これは非常に奇妙です。あなたは、ナビゲーションコントローラの視点に物事を直接加えるべきではありません。コードがビューコントローラ内にある場合、代わりに 'self.view.addSubview(topMenu)'でなければなりません。 – lukya

+0

私は、アプリケーションの複数のページで単一のテーブルを使用する必要がありますが、各ページのデータは異なります。それは私がUITableViewをサブクラス化した理由です。 – user1007352

答えて

0

あなたのコードには多くの問題があります。

最初に:自分の委任者であることはビューの責任ではありません。コントローラーがその責任を負います。 TableViewControllerを使用するとはるかに明確になります。

第2:イニシャライザが間違っています。あなたの初期化子は、これがうまくいく場合でも

init(frame: CGRect, style: UITableViewStyle, items : [String]) { 
     super.init(frame: frame, style:style) 

     self.scrollEnabled = false 
     self.dataSource = self 
     self.delegate = self 
     self.topArray = items 

     self.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") 
    } 

のようになりますようにあなたが同じことを行う必要があり、あなたはObjective Cの中で独自のコードをチェックする場合は、[::フレームスタイルスタイルスーパー initWithFrame]を呼んでいますデザインパターンに従うことをお勧めします。そうしないと、プロジェクトが将来管理することを非常に困難にするエントロピーの影響を受けます。

第三:私が前に言ったようにself.navigationController?.view.addSubview(topMenu)、私は、このビューを管理するビューコントローラを持つことをお勧めします、そしてスタックに、このビューコントローラをプッシュする代わりに、手動でサブビューとして、このビューを管理します。

これは私がここに見る問題の一部ですが、最も関連性の高いものです。クラッシュは、イニシャライザがそれ自身を連続的に呼び出すときの無限ループによるものです。私が助けてくれることを願っている

クラスMyTableView:のUITableView

0

あなたのサブクラスの宣言は次のようにする必要があります{

のvar arrItems:[文字列] = []

convenience init(frame:CGRect,style:UITableViewStyle, items:[String]){ 

    self.init(frame:frame,style:style) 
    self.arrItems = items 
} 
override init(frame: CGRect, style: UITableViewStyle) { 
    super.init(frame: frame, style: style) 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

}

関連する問題