UITableView中的几个单元格

时间:2013-10-19 16:18:18

标签: ios uitableview cell

我正在寻找如何创建几个单元格以转到不同的ViewControllers。

对于我的TableView,我使用的是UITableViewController的子类。

当我在下面的方法中选择2时,我只看到2个完全相同的单元格。我对此不感兴趣。我甚至不知道他们的IndexPath是为了改变他们的头衔。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}

当我尝试在我的TableView中添加另一个UITableViewCell时,它不会出现在iOS模拟器上,即使使用与我可以看到的第一个UITableViewCell相同的选项(相同的子类)。

感谢您的帮助。

编辑:这是我创建2个单元格但不起作用的新代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
static NSString *CellIdentifier = @"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[customCell alloc] init];

}

static NSString *CellIdentifier1 = @"Cell1";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil) {
    cell1 = [[customCell alloc] init];

}
// Configure the cell...

return cell;
}

3 个答案:

答案 0 :(得分:1)

您在tableView:cellForRowAtIndexPath:中定义了单元格,因此您应该为该方法提供实现。

tableView:numberOfRowsInSection:仅返回表格中的单元格数。

如果您需要更多帮助,请提供tableView:cellForRowAtIndexPath:的实施方案。这是典型的实现方式:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }

  ... customize your cell ...
}

编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *CellIdentifier = @"Cell2";
    static NSString *CellIdentifier1 = @"Cell1";

    if(indexPath.row == 0 ) {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
       cell = [[customCell alloc] init];

      }
    } else {

      UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
      if (cell1 == nil) {
        cell1 = [[customCell alloc] init];
      }
    }
    return cell;
}

答案 1 :(得分:0)

选择单元格时会调用此方法。您可以根据所选行

决定您想要做什么
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
       if (indexPath.row == 0)
           [self goToFirstViewController];
       else
       if(indexPath.row == 1)
        [self goToSecondViewController];
  }

答案 2 :(得分:0)

使用以下内容:

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section

此委托方法返回您在该特定部分中所需的行数。因此,如果您想要超过2行,或者您希望行数是动态的,您可以在AppDelegate或viewController类的init方法中创建NSArray,并返回numberOfRowsInSection方法中的数字,如

return [delegate numberOfNames];

在上面的例子中,我在AppDelegate中创建了一个数组,还有一个方法来返回我在该数组中的对象数,这样我就可以为我的表创建行数。

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath

此委托方法将显示您要在每个单元格中显示的内容。因此,继续在我的AppDelegate中创建的数组之后,我首先创建单元格,然后我将使用我在AppDelegate中创建的方法设置我想要在单元格上显示的文本,该方法将在接收NSInteger时返回NSString我可以遍历我的数组并相应地显示文本。

static NSString* MyIdentifier = @"Default";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if( cell == nil )
    {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
         cell.textLabel.text = [delegate nameAtIndex:indexPath.row];
    }

nameAtIndex是我在AppDelegate中创建的方法的名称,它将从我创建的NSArray中的特定索引(即行号)处返回NSString对象,以存储我的表的所有项目。

当用户点击创建的表中的任何行时,将调用此委托方法

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath

在这里,我将检查显示的文本是否与存储表中项目的AppDelegate中的数组中的任何项目匹配,并创建必要的视图。

UIViewController* viewController = nil ;
 NSString* nameInArray = [delegate nameAtIndex:indexPath.row] ;

    if( [nameInArray isEqualToString:@"firstName"] )
    {
        viewController = [[FirstViewController alloc] init];
    }
    else if( [nameInArray isEqualToString:@"secondName"] )
    {
        viewController = [[SecondViewController alloc] init];
    }
    else if( [nameInArray isEqualToString:@"thirdName"] )
    {
        viewController = [[ThirdViewController alloc] init];
    }

因此,使用这3个委托方法,您将能够使用创建的NSArray创建表,并能够根据用户选择的表中的哪个选项将用户重定向到viewController。如果您选择向表中添加更多行,则不必继续编辑委托方法,因为在设置表时返回数组的计数。

获取数组数据的数组和方法也可以在viewController中创建,不一定在AppDelegate中,以防你想知道。

方法如下:

-(NSInteger) numberOfNames
{
    return [myArray count];
}

-(NSString*) nameAtIndex: (NSInteger) index
{
    return [myArray objectAtIndex:index] ;
}

希望这有帮助! :)