我面对的一个模块包括在同一UITableView
中加载两个不同的UIViewController
我知道我在哪里做错了,问题是表视图方法中的行AtIndexpath的单元格。我在UITableView
中只得到一个UIViewController
,但我的secondviewcontroller不会在单元格中返回任何值。
这里是我的参考示例代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==table) {
return [self.arryData count];
}
else
return [tblArr count];
NSLog(@"tabeCount==>%lu",(unsigned long)tblArr.count);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier1 = @"Cell";
dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell;
mobilePlanDetailsCellTableViewCell *plan_cell;
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
}return cell;
if (tableView==planTable) {
plan_cell = [self.planTable dequeueReusableCellWithIdentifier:CellIdentifier1];
if (plan_cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
plan_cell = [nib objectAtIndex:0];
}
plan_cell.label.text = [[tblArr objectAtIndex:indexPath.row] objectForKey:@"Answer1"];
plan_cell.Label2.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer2"];
plan_cell.Label3.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer3"];
plan_cell.label4.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer4"];
}
return plan_cell;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView setBackgroundColor:[UIColor whiteColor]];
[cell setBackgroundColor:[UIColor whiteColor]];
}
答案 0 :(得分:2)
你犯了一个小错误,只需更改方法内的Return Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
if (tableView==table) {
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
return cell;
}
else {
mobilePlanDetailsCellTableViewCell *plan_cell = [self.planTable dequeueReusableCellWithIdentifier: CellIdentifier];
if (plan_cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"mobilePlanDetailsCellTableViewCell" owner:self options:nil];
plan_cell = [nib objectAtIndex:0];
}
plan_cell.label.text = [[tblArr objectAtIndex:indexPath.row] objectForKey:@"Answer1"];
plan_cell.Label2.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer2"];
plan_cell.Label3.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer3"];
plan_cell.label4.text = [[tblArr objectAtIndex:indexPath.row]objectForKey:@"answer4"];
return plan_cell;
}
}
答案 1 :(得分:1)
问题是您要将单元格退回到if (tableView==table)
之外,因此您的程序将无法访问该代码后的代码。
而不是
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
}return cell; //This line is wrong
你应该这样做:
if (tableView==table) {
cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.text = [self.arryData objectAtIndex:indexPath.row];
//Should be inside the if scope
return cell;
}
答案 2 :(得分:1)
为什么要进行这样复杂的编码,只需为表视图提供不同的标记值,然后返回if条件之外的单元格,这是错误的。请遵循以下代码。
像这样进行检查: -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.tag==1)// Table 1
{
//load cell as per your choice
//cell operations
return cell;
}
else // Table 2
{
//load cell as per your choice
//cell operations
return cell;
}