Iphone编程:多个UITableView从同一源读取?
这个问题与使用单独的委托/数据源时的UITableView问题有关,虽然我有一个不同的问题。我刚刚开始学习 iPhone 编程。
基本上我有一个带桌子的主视图。单击单元格时,会显示带有另一个表的子视图。
主视图表的数据源和委托被设置为文件的所有者,并且我在其中添加了必要的代码来处理表数据,一切都很好。 但是,当子视图中的第二个表似乎使应用程序崩溃时,我做了同样的事情,设置数据源并委托给文件的所有者,并重复与主视图的表相同的过程。我不知道为什么会发生这种情况。
子视图有其唯一的 nib/xib 文件和自己的出口。如果我没有将任何数据源附加到子视图的表,它将从主视图的表中获取数据;我不明白为什么会这样,因为我已将数据源设置为文件的所有者。
例如:FirstView
控制器有一个表FirstTable
,数据源和委托设置为Files
的所有者。我在 FirstView.m 中添加了以下内容:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"LibraryListingCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text =@"Table Cell";
return cell;
}
一切正常。 当我对第二个表和第二个视图重复此操作时,应用程序崩溃,说
reason: '-[UISectionRowData tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x69188d0'
我对第二个表执行了完全相同的操作:在 内实现了
并将第二个表的委托和数据源设置为文件的所有者。如果我删除第二个表的委托和数据源,应用程序不会崩溃,但在第二个视图中会有一个空表。numberOfRowsInSection
和 cellForRowAtIndexPatch
secondaryview.m
有什么建议吗?或者我在这里遗漏了一些关键概念?
This question is related to UITableView issue when using separate delegate/dataSource, though I have a different problem. I'm just starting to learn iPhone programming.
Basically I have one main view with a table. On the event of a cell click, a sub view with another table is shown.
The datasource and delegate for the main view's table are set as files' owner, and I have added the necessary code in there to handle the table data and everything is fine.
But, when the second table in the sub-view seems to crash the application, I did the same thing, set the datasource and delegate to the file's owner and repeated the same procedure as for the main view's table. I have no idea why this is happening.
The sub-view has its only nib/xib file and its own outlet. If i do not attach any datasource to the subview's table, it takes the data from the main view's table; I don't understand why that is, since I have set the datasource to be the file's owner.
For example: the FirstView
controller has a table FirstTable
, the datasource and delegate are set to the owner of Files
. I added the following in FirstView.m
:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"LibraryListingCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text =@"Table Cell";
return cell;
}
Everything works perfectly.
The moment I repeat this with a second table and a second view, the application crashes saying
reason: '-[UISectionRowData tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x69188d0'
I have done exactly the same for second table: implemented numberOfRowsInSection
and cellForRowAtIndexPatch
inside secondview.m
and set the second table's delegate and datasource to the file's owner. If I remove the delegate and datasource for the second table, the application doesn't crash but has an empty table in the second view.
Any suggestions? or am I missing some key concept here ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以对多个表使用相同的数据源和委托方法。
您必须提及您在哪个表中进行操作。
例如:
You can use same datasource and delegate methods for multiple tables.
You have to mention in which table you are doing operations.
For example:
这是主视图控制器
.h
文件。这是主视图控制器
.m
文件。添加一个名为子视图的视图控制器。这是
subview.h
:和
subview.m
:#导入“子视图.h”
#进口
@实现子视图
@synthesize contentView,tblVw,数组;
试试这个代码。这个应用程序是为 iPad 开发的。根据 iPhone 的需要更改尺寸。
This is the main View controller
.h
file.This is the main View controller
.m
file.Add a view controller called subview. Here's
subview.h
:And
subview.m
:#import "SubView.h"
#import
@implementation SubView
@synthesize contentView,tblVw,array;
Try this code. This app was done for the iPad. Change the dimensions as needed for the iPhone.
我遇到了完全相同的问题,并通过删除从表视图的数据源到文件所有者的连接来修复它。然后我粘贴了下面的代码来替换现有的
cellForRowAtIndexPath
。我必须更改数组名称以匹配我的数组,然后将数据源重新连接到文件所有者,然后它开始工作。我的函数代码中的某个地方肯定出了问题。I had the EXACT same issue and fixed it by deleting my connection from the table view's datasource to the files owner. Then I pasted in the code below replacing the existing
cellForRowAtIndexPath
. I had to change the array name to match my array and then reconnect the datasource to the files owner, and it started working. Must have been a snafu in my function code somewhere.