2009-08-24 286 views
17

我创建了一个UITableView,并希望特定的UITableViewCell在加载视图时显示为选中状态(蓝色)。默认情况下,如何选择UITableViewCell?

+0

看到这个问题: http://stackoverflow.com/questions/19295297/uitableviewcell- set-selected-beginning/25128427#25128427 – 2015-06-14 04:56:05

回答

0

使用的UITableViewCell方法

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 

信息here

+0

也许下面的方法更合适? http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableViewCell/setHighlighted:animated: – 2009-08-24 17:10:55

+0

哪种方法?链接不会去任何特定的方法 – Daniel 2009-08-24 17:27:16

+1

如果它ound,那一个突出显示它,并不选择它,我想如果这是所有提问者然后确定那更合适 – Daniel 2009-08-24 17:28:02

6

请谨慎使用此方法,因为以这种方式选择行Apple suggests against会显示“选定”状态。

相反,请考虑将单元格的accessoryType属性设置为UITableViewCellAccessoryCheckmark之类的内容。

+0

我想复制苹果用户界面时添加您添加/编辑日历应用程序中的事件并设置开始和结束日期/时间。我在其他地方看到了关于复选标记的评论,但我不确定它是否适用于这种情况(表格用于布置组件而不是呈现数据列表)。 – user119857 2009-08-24 19:55:40

+0

这是一个有趣的观点。在这种情况下,它们不需要为每个Start和End都有一个子视图,因为它们都代表相同类型的数据,并由同一种UIPickerView修改。所以这将是一个明智的用途。他们很可能使用UITableView的selectRowAtIndexPath:animated:scrollPosition:方法...请参阅http://tinyurl.com/yd9zs65 – 2009-10-07 19:38:11

1

绝对要小心。我相信你有一个很好的理由,但仔细看一下苹果提供的人机界面指南文档。应用因不取消选择表格行而遭到拒绝。我鼓励你找到HIG的适当部分,并看到苹果提供任何建议。

41
-(void)viewWillAppear:(BOOL)animated { 
    // assuming you had the table view wired to IBOutlet myTableView 
    // and that you wanted to select the first item in the first section 
    [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
          animated:NO 
         scrollPosition:UITableViewScrollPositionTop]; 
} 

我使用这种技术来选择两个UITableViewCells一个让用户知道哪个单元格的表格下方的UIDatePicker查看它会影响。当您创建新事件并设置日期时,苹果会在日历应用程序中使用此技术。

+1

1.这不适用于我。 2.苹果说你应该使用复选标记来表示一个项目的选择状态。 – 2014-04-26 06:04:09

3

你应该把它放在viewWillAppear

[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
         animated:NO 
        scrollPosition:0]; 

如果试图在cellForRowAtIndexPath:选择它,那么它不会采取必要的风格。

0

有时,当您不在UIViewController中时,您没有viewWillAppear,有时,您以编程方式创建了UITableView

最简单的解决方案是实现此delegate方法:

- (void)tableView:(UITableView *)tableView 
     willDisplayCell:(UITableViewCell *)cell 
     forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (self.selectedIndex == indexPath.row) { 
     cell.selected = YES; 
    } 
} 

这是因为还没有显示单元在cellForRowAtIndexPath不起作用。并且仅在显示这个方法时调用setSelected方法。

1

使用此代码默认情况下,表视图,选择单元格,indexPath可以改变根据您的需要

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]; 
    [theTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom]; 
} 
相关问题