【问题标题】:How to load UITableView having separate subclass for delegate and data Source?如何为委托和数据源加载具有单独子类的 UITableView?
【发布时间】:2015-10-08 11:10:11
【问题描述】:

有一个UIViewController,由一个UITextField 和一个UITableView 组成。我有一个单独的UIViewTableViewController 子类,用于支持UIViewController(默认)中UITableView 的委托和数据源。我可以从UITexField 获取文本到SecondViewController 并将其保存到NSMutableArray,但这不会传递到UITableView。如果我只是将一个字符串传递给 *cell,它会显示在 UITableView 中。 代码如下:

ViewController.h

    #import <UIKit/UIKit.h>
    #import "SecondTableViewController.h"

    @interface ViewController : UIViewController{
        SecondTableViewController *tableController;
    }
    @end

**ViewController.m**

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    tableController = [[SecondTableViewController alloc] init];
    tableController.taskArray = [[NSMutableArray alloc]init];

    self.table.dataSource = tableController;
    self.table.delegate = tableController;
    self.textField.delegate = tableController;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

SecondTableViewController.h

#import <UIKit/UIKit.h>


@interface SecondTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@property NSMutableArray *taskArray;
@property NSString *taskString;


@end

SecondViewController.m

#import "SecondTableViewController.h"
#import "ViewController.h"

@implementation SecondTableViewController


-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    self.taskString = [textField text];
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField {

    [self.taskArray addObject:self.taskString];
    [textField setText:@""];
    NSLog(@"array is %@", self.taskArray);
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self
                                             .self.taskArray.count-1 inSection:0]] withRowAnimation:
    UITableViewRowAnimationLeft];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.taskArray.count;
    }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                }
        cell.textLabel.text = self.taskArray[indexPath.row];
    NSLog(@"cell ident %@", self.taskArray[indexPath.row]);

    return cell;
}




@end

【问题讨论】:

    标签: ios objective-c iphone uitableview uiviewcontroller


    【解决方案1】:

    您在 SecondTableViewController 中对 tableView 的引用并未指向您在原始 UIViewController 中创建的 tableView var,因此 cellForRowAtIndexPath 不会对 tableView var 做任何事情。

    TableViewController 类有一个名为tableView 的属性,该属性已经绑定到该类,并引用其父类,然后该类作为委托和数据源。您已断开与委托和数据源的链接,但未断开与 tableView 属性本身的链接。

    如果您想将委托和数据源与tableview 所在的类分开,那么您可以这样做,但您不需要使用 UITableViewController 类。您可以创建一个自定义类继承 NSObject 并在其中包含所有这些委托和数据源调用。如果你这样做了,那么你设置 tableView 的委托和数据源的代码将如下所示

     SeparateDataSourceClass *separateClass = [SeparateDataSourceClass new];
     self.table.dataSource = separateClass;
     self.table.delegate = separateClass;
     self.textField.delegate = separateClass;
    

    类似的东西,但你可能明白了。

    【讨论】:

      【解决方案2】:

      我不知道你为什么要这样做。我的意思是 View Controller 2 正在控制 View Controller 1 的外观!!!!你不要这样做!

      但是,您的问题是 SecondTableViewController 中的 self.tableView 不是您为其设置委托的表视图。 SecondTableViewController 中的 self.tableView 是默认表视图,它是 UITableViewController 的子类。

      要解决这个问题,有几个解决方案:

      解决方案 1: 摆脱这种混乱的视图层次结构并拥有相同的视图控制器 管理这些东西。

      解决方案 2: 在右表视图对象上调用 insertRowsAtIndexPaths。为此,您可以将 tableView 引用从 ViewController 传递到 SecondTableViewController

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-29
        • 2012-07-01
        • 1970-01-01
        • 2016-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        相关资源
        最近更新 更多