Ios 如何使用iPhone SDK创建动态UITableView单元格

Ios 如何使用iPhone SDK创建动态UITableView单元格,ios,iphone,xcode,uitableview,Ios,Iphone,Xcode,Uitableview,在我的应用程序中,我有UITableView,其中第一个单元格和最后三个单元格的内容是固定的。但根据数组计数和内容,这些单元格之间的内容是不同的 例如:第一个单元格+数组计数+最后三个单元格 数组计数始终不同。 如何创建如上所述的表视图。在numberOfRows方法中,返回值为 4 + [array count]; 在cellforrowatinexpath中,打开indexPath.row并相应地填充单元格内容。在numberOfRows方法中,返回值如下 4 + [array count

在我的应用程序中,我有
UITableView
,其中第一个单元格和最后三个单元格的内容是固定的。但根据数组计数和内容,这些单元格之间的内容是不同的

例如:第一个单元格+数组计数+最后三个单元格

数组计数始终不同。
如何创建如上所述的表视图。

numberOfRows
方法中,返回值为

4 + [array count];

cellforrowatinexpath
中,打开
indexPath.row
并相应地填充单元格内容。

numberOfRows
方法中,返回值如下

4 + [array count];

cellforrowatinexpath
中,打开
indexPath.row
并相应地填充单元格内容。

//第一个单元格的工作代码+[dynamic array]+最后三个固定单元格

  #import "tableTOViewController.h"

@interface tableTOViewController ()

@end

@implementation tableTOViewController

- (void)viewDidLoad
{
 myArray=[[NSArray alloc] init];
 myArray=[NSArray arrayWithObjects:@"Aman",@"Atul",@"Karan",@"Yen",@"Chan",@"Lee", nil];

 lastFixedArray=[[NSArray alloc] init];
 lastFixedArray=[NSArray arrayWithObjects:@"Snakes",@"Food",@"Drink", nil];

    tblView.delegate=self;
    tblView.dataSource=self;
    [super viewDidLoad];
}

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return [myArray count]+4;
}

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

 if (cell==nil) {
  cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }

 if (indexPath.row==0) 
  cell.textLabel.text=@"First Cell";
 else if (indexPath.row>[myArray count])
  cell.textLabel.text=[lastFixedArray objectAtIndex:([indexPath row]-[myArray count]-1)];
 else
  cell.textLabel.text=[myArray objectAtIndex:indexPath.row-1];

 return cell;
}


@end

//第一个单元格+[动态数组]+最后三个固定单元格的工作代码

  #import "tableTOViewController.h"

@interface tableTOViewController ()

@end

@implementation tableTOViewController

- (void)viewDidLoad
{
 myArray=[[NSArray alloc] init];
 myArray=[NSArray arrayWithObjects:@"Aman",@"Atul",@"Karan",@"Yen",@"Chan",@"Lee", nil];

 lastFixedArray=[[NSArray alloc] init];
 lastFixedArray=[NSArray arrayWithObjects:@"Snakes",@"Food",@"Drink", nil];

    tblView.delegate=self;
    tblView.dataSource=self;
    [super viewDidLoad];
}

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return [myArray count]+4;
}

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

 if (cell==nil) {
  cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }

 if (indexPath.row==0) 
  cell.textLabel.text=@"First Cell";
 else if (indexPath.row>[myArray count])
  cell.textLabel.text=[lastFixedArray objectAtIndex:([indexPath row]-[myArray count]-1)];
 else
  cell.textLabel.text=[myArray objectAtIndex:indexPath.row-1];

 return cell;
}


@end

如果要将子视图添加到最后三个单元格中,则可以使用以下条件

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

 if (cell==nil) {
  cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }

 if (indexPath.row==0) 
  cell.textLabel.text=@"First Cell";
 else if (indexPath.row>[myArray count])
 {
  int k=([indexPath row]-[myArray count]-1);

  switch (k) {
   case 0:
    [cell.contentView addSubview:thirdLastView];
    break;
   case 1:
    [cell.contentView addSubview:secondLastView];
    break;
   case 2:
    [cell.contentView addSubview:lastView];
    break;
  }
    cell.textLabel.text=[lastFixedArray objectAtIndex:k];
 }
 else
  cell.textLabel.text=[myArray objectAtIndex:indexPath.row-1];

 return cell;
}

如果要将子视图添加到最后三个单元格中,则可以使用以下条件

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

 if (cell==nil) {
  cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }

 if (indexPath.row==0) 
  cell.textLabel.text=@"First Cell";
 else if (indexPath.row>[myArray count])
 {
  int k=([indexPath row]-[myArray count]-1);

  switch (k) {
   case 0:
    [cell.contentView addSubview:thirdLastView];
    break;
   case 1:
    [cell.contentView addSubview:secondLastView];
    break;
   case 2:
    [cell.contentView addSubview:lastView];
    break;
  }
    cell.textLabel.text=[lastFixedArray objectAtIndex:k];
 }
 else
  cell.textLabel.text=[myArray objectAtIndex:indexPath.row-1];

 return cell;
}


你试过什么东西吗?然后告诉我们…不,我不知道该怎么做。在numberofrowsinsection方法中,返回为([array count]+6)。在cellForRowAtIndex中:检查indexPath.row是否小于3或大于[arrayCount-1],并为单独的indexPath行值编写一个逻辑,以显示您想要的内容。您尝试过什么吗?然后告诉我们…不,我不知道该怎么做。在numberofrowsinsection方法中,返回为([array count]+6)。在cellForRowAtIndex中:检查indexPath.row是否小于3或大于[arrayCount-1],并为单独的indexPath行值编写一个逻辑,以显示代码所需的..+1。为什么不在else语句中尝试else if condition而不是if else。最近,每次出现问题时,我的xcode都会退出,因此我无法检查每个条件,但您可以像条件为false一样运行和编辑。@R.AI说只需更改上面答案中的代码。无论如何,它仍然可以工作。所以没问题,这段代码在@user1673099下不起作用吗?如果工作,那么接受它。太好了!!你的代码正在运行!!但我需要一些修改,比如我还想分别识别最后三个单元格。就像最后一个用来喝水的牢房,第二个用来吃饭,第三个用来养蛇。但最后三个单元格总是出现在表视图中。。救救我+代码为1。为什么不在else语句中尝试else if condition而不是if else。最近,每次出现问题时,我的xcode都会退出,因此我无法检查每个条件,但您可以像条件为false一样运行和编辑。@R.AI说只需更改上面答案中的代码。无论如何,它仍然可以工作。所以没问题,这段代码在@user1673099下不起作用吗?如果工作,那么接受它。太好了!!你的代码正在运行!!但我需要一些修改,比如我还想分别识别最后三个单元格。就像最后一个用来喝水的牢房,第二个用来吃饭,第三个用来养蛇。但最后三个单元格总是出现在表视图中。。帮助我!!再次感谢您的回复!!再次感谢您的回复!!