私はこれを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];
は明らかに、あなたは自由ですあなたが何をしたいのですが、あなたはUITableViewをサブクラス化しますか?そして、あなたがしたとしても、そのクラスをIB/Storyboard –
'self.navigationController?.view.addSubview(topMenu)'に使うことができます。これは非常に奇妙です。あなたは、ナビゲーションコントローラの視点に物事を直接加えるべきではありません。コードがビューコントローラ内にある場合、代わりに 'self.view.addSubview(topMenu)'でなければなりません。 – lukya
私は、アプリケーションの複数のページで単一のテーブルを使用する必要がありますが、各ページのデータは異なります。それは私がUITableViewをサブクラス化した理由です。 – user1007352