代码之家  ›  专栏  ›  技术社区  ›  Veeru

Iphone编程:多个UITableViews从同一个源读取?

  •  1
  • Veeru  · 技术社区  · 14 年前

    UITableView issue when using separate delegate/dataSource ,尽管我有不同的问题。我刚开始学iPhone编程。

    主视图表的数据源和委托被设置为文件的所有者,我在其中添加了必要的代码来处理表数据,一切都很好。 但是,当子视图中的第二个表似乎使应用程序崩溃时,我做了同样的事情,设置数据源并将其委托给文件的所有者,并重复了与主视图的表相同的过程。我不知道为什么会这样。

    例如: 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 secondview.m 并将第二个表的委托和数据源设置为文件的所有者。如果删除第二个表的委托和数据源,应用程序不会崩溃,但在第二个视图中有一个空表。

    有什么建议吗?或者是我遗漏了一些关键的概念?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Gilles 'SO- stop being evil'    13 年前

    这是主视图控制器 .h 文件。

    #import <UIKit/UIKit.h>
    #import "SubView.h"
    @interface StackOverTableSubViewViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
    {
        UIView *contentView;
        UITableView *tblVw;
        NSMutableArray *array;
        SubView *SubViewObj;
    }
    @property(nonatomic,retain) UIView *contentView;
    @property(nonatomic,retain) UITableView *tblVw;
    @property(nonatomic,retain) NSMutableArray *array;
    @property(nonatomic,retain) SubView *SubViewObj;
    @end
    

    .m 文件。

    #import "StackOverTableSubViewViewController.h"
    @implementation StackOverTableSubViewViewController
    @synthesize contentView,tblVw,array,SubViewObj;
    
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView
    {
        contentView=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        contentView.autoresizingMask=(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        contentView.autoresizesSubviews=YES;
        contentView.backgroundColor=[UIColor whiteColor];
    
        tblVw=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
        tblVw.dataSource=self;
        tblVw.delegate=self;
        tblVw.scrollEnabled=YES;
    
        array=[[NSMutableArray alloc]init];
        [array addObject:@"Row1"];
        [array addObject:@"Row2"];
        [array addObject:@"Row3"];
        [array addObject:@"Row4"];
        [array addObject:@"Row5"];
        [array addObject:@"Row6"];
        [array addObject:@"Row7"];
        [array addObject:@"Row8"];
        [array addObject:@"Row9"];
        [array addObject:@"Row10"];
        [array addObject:@"Row11"];
        [array addObject:@"Row12"];
        [contentView addSubview:tblVw];
        self.view=contentView;
    }
    
    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
    {
        return [array count];
    }
    
    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CellIdentifier";
    
        // Dequeue or create a cell of the appropriate type.
        // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 0;
            [cell.textLabel sizeToFit];
        }
        cell.textLabel.text=[array objectAtIndex:indexPath.row];
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        SubViewObj=[[SubView alloc]init];
        [self.view addSubview:SubViewObj.view];
    }
    
    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    */
    
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (void)dealloc {
        [contentView release];
        [SubViewObj release];
        [tblVw release];
        [array release];
        [super dealloc];
    }
    
    @end
    

    添加名为subview的视图控制器。这是 subview.h

    #import <UIKit/UIKit.h>
    @interface SubView : UIViewController <UITableViewDelegate,UITableViewDataSource>
    {
        UIView *contentView;
        UITableView *tblVw;
        NSMutableArray *array;
    }
    @property(nonatomic,retain) UIView *contentView;
    @property(nonatomic,retain) UITableView *tblVw;
    @property(nonatomic,retain) NSMutableArray *array;
    @end
    

    以及 subview.m : #导入“SubView.h” @实现子视图 @合成contentView、tblVw、array;

    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView {
        contentView=[[UIView alloc]initWithFrame:CGRectMake(200, 10, 300, 600)];
        contentView.autoresizingMask=(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        contentView.autoresizesSubviews=YES;
        contentView.backgroundColor=[UIColor whiteColor];
    
        tblVw=[[UITableView alloc]initWithFrame:CGRectMake(200, 10, 300, 600) style:UITableViewStylePlain];
        tblVw.dataSource=self;
        tblVw.delegate=self;
        tblVw.scrollEnabled=YES;
        tblVw.layer.borderWidth=4.0;
        tblVw.layer.borderColor=[[UIColor redColor]CGColor];
        array=[[NSMutableArray alloc]init];
        [array addObject:@"Data1"];
        [array addObject:@"Data2"];
        [array addObject:@"Data3"];
        [array addObject:@"Data4"];
        [array addObject:@"Data5"];
        [array addObject:@"Data6"];
        [array addObject:@"Data7"];
        [array addObject:@"Data8"];
        [array addObject:@"Data9"];
        [array addObject:@"Data10"];
        [array addObject:@"Data11"];
        [array addObject:@"Data12"];
    
        [contentView addSubview:tblVw];
        self.view=contentView;
    
    }
    
    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    */
    
    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
    {
        return [array count];
    }
    
    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CellIdentifier";
    
        // Dequeue or create a cell of the appropriate type.
        // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 0;
            [cell.textLabel sizeToFit];
        }
        cell.textLabel.text=[array objectAtIndex:indexPath.row];
        return cell;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Overriden to allow any orientation.
        return YES;
    }
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    

    试试这个代码。这个应用程序是为iPad开发的。根据需要更改iPhone的尺寸。

        2
  •  3
  •   Gilles 'SO- stop being evil'    13 年前

    你必须提到你在哪个表中做操作。 例如:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([tableView isEqual:TableView1])
        {
           //do work for tableview1
         }
         else if([tableView isEqual:TableView2])
       {
          //do operations for tableview2
       }
    }
    
        3
  •  1
  •   Gilles 'SO- stop being evil'    13 年前

    cellForRowAtIndexPath . 我必须更改数组名以匹配我的数组,然后将数据源重新连接到文件所有者,然后它开始工作。一定是我的函数代码出了问题。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CellIdentifier";
        // Dequeue or create a cell of the appropriate type.
        // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 0;
            [cell.textLabel sizeToFit];
        }
        cell.textLabel.text=[array objectAtIndex:indexPath.row];
        return cell;
    }