iOS 5,Storyboards,ARC:创建和设置自定义委托和数据源来编程。创建UITableView [英] iOS 5, Storyboards,ARC: Creating and setting custom delegate and datasource to program. created UITableView

查看:96
本文介绍了iOS 5,Storyboards,ARC:创建和设置自定义委托和数据源来编程。创建UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的前身是这里 a>。

The prequel of this problem is here.

我试图创建和设置一个自定义委托和数据源到我的程序化创建的UITableView。我已经google了,但是找不到任何明确的解决方案我的问题。

I am trying to create and set a custom delegate and datasource to my programmatically created UITableView. I've googled around, but couldn't find any clear solution for my problem.

同时,我创建了一个新类符合UITableViewDelegate,UITableViewDataSource
协议。在此类中:

Meanwhile, I've created a new class that conforms to UITableViewDelegate,UITableViewDataSource protocols. In this class:

tableView numberOfRowsInSection:20
tableView cellForRowAtIndexPath:cell.textLabel.text = @Nominals;

tableView numberOfRowsInSection: 20 tableView cellForRowAtIndexPath: cell.textLabel.text=@"Nominals";

包含UIViews的类:

Class that contains UIViews:

创建UITableView的方法:

Method that creates UITableView:

-(IBAction)segmentValueChaged:(id)sender
{
if(self.segment.selectedSegmentIndex==0)
{
    [self.coinageView removeFromSuperview];
    [self.view addSubview:nominalsView];
    [self populateNominals:self.subCountryID];
}
else 
{
    [self.nominalsView removeFromSuperview];
    [self.view addSubview:coinageView];
    [self populateCoinages:self.subCountryID];
}
}

-(void)populateNominals:(int)subCountryID
{
NominalsTableViewDelegate *del=[[NominalsTableViewDelegate alloc]init];
UITableView *nominalsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 372) style:UITableViewStylePlain];
[nominalsTableView setDelegate:del];
[nominalsTableView setDataSource:del];
[self.nominalsView addSubview:nominalsTableView];
[nominalsTableView reloadData];
}

最后,我得到EXC_BAD_ACCESS。
邪恶在[nominalsTableView setDelegate:del]; [nominalsTableView setDataSource:del];行。他们有什么问题。

Finally, I'm getting EXC_BAD_ACCESS. The evil is in [nominalsTableView setDelegate:del]; [nominalsTableView setDataSource:del]; rows. What's wrong with them.

帮助非常感谢。提前感谢。

Help is much appreciated. Thanks in advance.

推荐答案

我不确定您是否要向当前视图添加新的UITableView,假设您正在这样做。

I am not sure if you are adding a new UITableView to your current view or not, but I will assume you are doing so.

如果您有一个类符合UITableViewDelegate。但是当我需要以编程方式创建一个UITableView时,我创建一个新的UITableView类(使用.h和.m),然后创建一个MutableArray并将其作为父视图的属性展示,以便可以设置数据源。

If you have a class that conforms to the UITableViewDelegate. But when i needed to create a UITableView programmatically, I create a new UITableView class (with the .h and .m) then make a MutableArray and exposing it as a property to the parent view so it can set the data source.

从那里,你可以创建一个类的实例和数据源(你从子对象中暴露)。最后,您将视图添加到当前视图。这种方法你不需要设置委托,因为你的子类符合tableview委托协议。

From there, you can create an instance of the class along with the datasource (which you exposed from the child object). Finally, you then add the view onto your current view. This method you dont need to set the delegate because your child class conforms to the tableview delegate protocol.

如果你只是修改当前tableview中的数据,那么,你使用a NSMutableArray ,然后更改其中的数据。然后做一个

If you are just modifying the data inside the current tableview then, you use a NSMutableArray and then change the data in it. After then do a

      [self.tableView reloadData];

希望这有助于!

EDITED

我可能误解了你的问题,你(最可能需要做的)是创建一个委托类的属性,然后创建一个实例

I might have misunderstood your question, what you (most likely) need to do is create a property of the delegate class then create an instance of your delgate class and assign it to the property.

然后执行

      [myTableView setDelegate:self.myProperty];
      [myTableView setDatasource:self.myProperty];

我相信这会解决你访问不好的问题。

This, I believe, would solve you bad access problem.

已修改

在.view的类中创建属性:

Create a property inside your .h of the tableview class as such:

    @property (nonatomic, retain) NominalsTableViewDelegate *myDelegate;

然后在里面的.m文件,你做类似这样的事情:

From there then inside your .m file, you do something similar to this:

NominalsTableViewDelegate* delegateClass = [[NominalsTableViewDelegate alloc] init];
[self setMyDelegate:delegateClass];
[delegateClass release];

然后你可以设置你的tableview数据源:

Then you can set your tableview datasource as such:

[myTableView setDelegate:self.myDelegate];
[myTableView setDatasource:self.myDelegate];

请注意 :到一台机器来测试这个,但只是一些东西指向你。

Note: I currently have no access to a machine to test this, but just something to point you towards.

这篇关于iOS 5,Storyboards,ARC:创建和设置自定义委托和数据源来编程。创建UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆