【问题标题】:Creating complex UITableView with multiple different custom cells使用多个不同的自定义单元创建复杂的 UITableView
【发布时间】:2012-10-29 04:33:07
【问题描述】:

我在具有自定义 UITableViewCell 的表视图中创建侧边菜单。

我创建了一个可重复使用的单元格,用于以下用途:

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

if (cell == nil) {

    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"LeftMenuTableViewCell" owner:nil options:nil];

    for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (LeftMenuTableViewCell*)view;
            [cell.displayName setText:[NSString stringWithFormat:@"Cell 1"];

        }
    }
}

return cell;

}

我想知道如何为此表格创建其他单元格。该表将类似于 Facebook/路径侧菜单。例如 我的简历 设置 帮助 注销

每个都具有相同的设计,但标签和旁边的图标的文本不同。还会在右侧加载不同的视图控制器。有人可以解释这是如何实现的吗?或者提供任何教程的链接来解释如何使用多个不同的单元格制作自定义表格视图,我是否需要复制单元格 .h、.m 和 NIB 文件并分别创建每个自定义 UITabelViewCell?

【问题讨论】:

    标签: ios ios5 uitableview


    【解决方案1】:

    使用相同的单元格类并将其放置为(由于单元格没有设计更改,只有文本和图像在更改),

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      //some code
      if () { //condition 1 
         cell.textLabel.text = @"My Profile";
         cell.imageView.image = [UIImage imageNamed:@"MyProfile.png"];
      } else if () { //condition 2
         cell.textLabel.text = @"Settings";
         cell.imageView.image = [UIImage imageNamed:@"Settings.png"];
      } else if () { //condition 3
         cell.textLabel.text = @"Logout";
         cell.imageView.image = [UIImage imageNamed:@"Logout.png"];
      } 
      //some code
    }
    

    didSelectRowAtIndexPath方法中,

    if () { //condition 1, say indexpath.row == 0
      //go to firstviewcontroller
    } else if () { //condition 2, say indexpath.row == 1
      //go to secondviewcontroller
    } else if () { //condition 3, say indexpath.row == 2
      //go to thirdviewcontroller
    }
    

    这应该适合你吧?

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多