【发布时间】:2013-11-12 11:46:19
【问题描述】:
我正在尝试在情节提要的单个 segue 中实现 3 个表。 选择一个表时,它会加上另一个表的视图,同样还有一个。 以下代码用于一个表格,每个表格的单元格格式不同,行也不同。那么如何通过编码为每个表设置不同的行数等来区分每个表?
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell2";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1==nil)
{
cell1=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
temp=[array objectAtIndex:indexPath.row];
UILabel *Label1 = (UILabel *)[cell1 viewWithTag:4];
Label1.text = temp.Title;
UILabel *Label2 = (UILabel *)[cell1 viewWithTag:6];
Label2.text = temp.Title;
UITextField *textfield1 = (UITextField *)[cell1 viewWithTag:5];
textfield1.text =temp.description;
UILabel *Label3 = (UILabel *)[cell1 viewWithTag:7];
Label3.text = temp.Title;
return cell1;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.showlist=[[ShowList alloc]initWithNibName:@"ShowList" bundle:nil];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
ShowlistIndex=indexPath.row;
_secondview.hidden=NO;
}
【问题讨论】:
-
与其将所有
UITableViews 放入一个UIViewController,不如创建一个UIViewController和3 个UITableViewControllers。添加每个UITableViewController作为UITableViewController的子视图控制器。这允许您单独实现每个UITableViewController,而无需在每个表视图委托方法中使用一堆 if/else if。 -
@bobnoble 我想到了这一点,但我在实现子视图控制器时遇到了一些问题。以前没做过。这就是为什么我试图把它放在子视图中。无论如何,谢谢你的观点。
标签: ios uitableview uiviewcontroller multiple-tables