Iphone 向2个表添加数据
我正在尝试将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
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; }
}
尝试按照以下步骤操作,使两个表视图具有相同的
委托
和数据源
标记
属性,并在这两个值上定义常量。这使得代码一致标记
属性值#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; }
}
尝试按照以下步骤操作,使两个表视图具有相同的
委托
和数据源
标记
属性,并在这两个值上定义常量。这使得代码一致标记
属性值#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
条件中返回了相同的代码,表名不同。我在问题中添加了一个新的代码块。希望这有助于发现问题所在