【问题标题】:Remove Gray UITableView Index Bar删除灰色 UITableView 索引栏
【发布时间】:2011-03-12 11:41:07
【问题描述】:

我正在制作一个带有几个部分 (2) 的 UITableView 的应用程序,当我运行时,表格视图的侧面有这个烦人的灰色索引栏,就像“iPod”应用程序中的那个一样,它有但是里面有2个选项。我的问题是,如何隐藏“索引栏”,因为这是不必要的空间浪费?

示例:

代码sn-ps:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [sections count];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return sections;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [sections objectAtIndex:section];
}


// Customize the appearance of table view cells.
- (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];
    }

    // Configure the cell.
    cell.textLabel.text = [content objectAtIndex:(indexPath.row + [[sectionAmounts objectAtIndex:indexPath.section] intValue])];
    tableView.showsVerticalScrollIndicator = NO;
    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    content = [NSArray arrayWithObjects:@"Sphere", @"Cylinder", @"Circle", nil];
    sections = [NSArray arrayWithObjects:@"3d", @"2d", nil];
    sectionAmounts = [NSArray arrayWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:2], nil]; //Second number is objects in first section... odd huh?
    self.tableView.showsVerticalScrollIndicator = NO;
    [content retain];
    [sections retain];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

(请注意我的奇数...)

你好

【问题讨论】:

  • 你想完全去掉部分索引还是只去掉“滚动条”?

标签: objective-c scrollbar hide uitableview


【解决方案1】:

使用UITableViewsectionIndexBackgroundColor属性将索引栏的颜色设置为清除颜色,因为隐藏滚动条也会隐藏索引。

将其添加到您的viewDidLoad:,如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor]; //Replace it with your desired color
}

【讨论】:

    【解决方案2】:

    您只需使用下面的代码,灰色条就会消失......

        tableViewBrowse.sectionIndexTrackingBackgroundColor = [UIColor clearColor];
    

    【讨论】:

      【解决方案3】:

      我有类似的问题,但必须处理我的页眉/页脚。基本上我只是删除了以下方法

      - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
      {
          return @" "; //@"Top";
      }
      
      - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
      {
          return @" "; //@"Bottom";
      }
      

      【讨论】:

        【解决方案4】:

        那个“滚动条”是你的索引条,假设你在谈论巨大的灰色东西。

        从 sectionIndexTitlesForTableView: 返回 nil 并且它会消失。

        【讨论】:

        • 我要离开一段时间,但等我回来我会试试这个。 ~通过我的 iPod touch
        【解决方案5】:

        我试过了,但它对我不起作用。 所以我浏览了表格视图属性,得到了这个。

        self.tableView.showsVerticalScrollIndicator = NO;
        

        像魅力一样工作。

        *对于任何想在未来这样做的人。

        【讨论】:

        • 嗯,没用。在 iOS 5.1 上使用基本表格视图进行了尝试
        • 它应该可以工作。 tableview 是 uiscrollview 的子类,这个属性是 uiscrollview 的一部分,它仍然在类引用中列出。我刚刚对其进行了测试,它对我有用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-11
        • 2015-12-17
        • 1970-01-01
        相关资源
        最近更新 更多