Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/47.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 向2个表添加数据_Iphone_Objective C_Ios_Uitableview - Fatal编程技术网

Iphone 向2个表添加数据

Iphone 向2个表添加数据,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我正在尝试将2个UITableView添加到我的UIViewController。我需要向这些表中添加不同的数据 这就是我添加这两个表的方式(这段代码是添加到ViewDidLoad方法中的) 然后是另一张桌子 self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,100) style:UITableViewStylePlain] ; self.tableView1 .dataSource = self; s

我正在尝试将2个
UITableView
添加到我的
UIViewController
。我需要向这些表中添加不同的数据

这就是我添加这两个表的方式(这段代码是添加到ViewDidLoad方法中的)

然后是另一张桌子

self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,100) style:UITableViewStylePlain] ;
self.tableView1 .dataSource = self;
self.tableView1 .delegate = self;
定义如下的节数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==tableView1) {
        return 12;
    } 
    else { return 10; }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
    // ...... more code here
    if (tableView == self.tableView1) {     
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];        
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text=@"Cells .... ";
    } 
    else{
        // the remaining code here.. i am populating the cell as in the previous `IF` condition.
    }
}
问题是,我只得到了第一个表的填充,而不是第二个表。为什么会这样?我怎样才能解决这个问题

编辑: 我还添加了以下代码,希望能有所改变

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==tableView1) {
        return 1;
    }
    else if (tableView==tableView2) { return 0; }
    else { return 0; }
}

尝试按照以下步骤操作,使两个表视图具有相同的
委托
数据源

  • 设置表视图的
    标记
    属性,并在这两个值上定义常量。这使得代码一致

  • 在视图控制器子类中实现的委托和数据源方法中,根据定义的常量测试
    标记
    属性值

  • 不要为表视图返回0节,它根本不会显示任何单元格

  • 例如:

    #define TV_ONE 1
    #define TV_TW0 2
    
    // setting the tag property
    self.tableView1 = [[UITableView alloc]
                       initWithFrame:CGRectMake(0,0,320,100)
                               style:UITableViewStylePlain];
    self.tableView1.tag = TV_ONE;
    self.tableView1.dataSource = self;
    self.tableView1.delegate = self;
    // the same for tableView2 using TV_TWO
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        if (tableView.tag == TV_ONE) {
            return 1;
        }
        else if (tableView.tag == TV_TWO) {
            return 1; // at least one section
        }
        else { return 0; }
    }
    

    尝试按照以下步骤操作,使两个表视图具有相同的
    委托
    数据源

  • 设置表视图的
    标记
    属性,并在这两个值上定义常量。这使得代码一致

  • 在视图控制器子类中实现的委托和数据源方法中,根据定义的常量测试
    标记
    属性值

  • 不要为表视图返回0节,它根本不会显示任何单元格

  • 例如:

    #define TV_ONE 1
    #define TV_TW0 2
    
    // setting the tag property
    self.tableView1 = [[UITableView alloc]
                       initWithFrame:CGRectMake(0,0,320,100)
                               style:UITableViewStylePlain];
    self.tableView1.tag = TV_ONE;
    self.tableView1.dataSource = self;
    self.tableView1.delegate = self;
    // the same for tableView2 using TV_TWO
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        if (tableView.tag == TV_ONE) {
            return 1;
        }
        else if (tableView.tag == TV_TWO) {
            return 1; // at least one section
        }
        else { return 0; }
    }
    

    如何将TableView添加到superview?嗯。。我是按照上面的代码做的。它也会显示出来。我无法填充它。需要帮助!确实要在else部分返回任何内容吗?是的,我返回了,在
    if
    条件中返回了相同的代码,表名不同。我在问题中添加了一个新的代码块。希望有助于发现问题所在如何将TableView添加到superview?嗯。。我是按照上面的代码做的。它也会显示出来。我无法填充它。需要帮助!确实要在else部分返回任何内容吗?是的,我返回了,在
    if
    条件中返回了相同的代码,表名不同。我在问题中添加了一个新的代码块。希望这有助于发现问题所在