我有一個基於視圖的應用程序,我將一個tableview作爲子視圖添加到主視圖。我已採取UITableViewDelegate
響應表方法。一切工作正常,但我想選擇第一行或UITableView
作爲默認選定(突出顯示)。在UITableView中選擇第一行作爲默認值
請幫助我,我需要什麼代碼以及需要放在哪裏。
我有一個基於視圖的應用程序,我將一個tableview作爲子視圖添加到主視圖。我已採取UITableViewDelegate
響應表方法。一切工作正常,但我想選擇第一行或UITableView
作爲默認選定(突出顯示)。在UITableView中選擇第一行作爲默認值
請幫助我,我需要什麼代碼以及需要放在哪裏。
我們根據它是否是第一個細胞......中間細胞或最後一個細胞,爲細胞使用自定義背景圖像。這樣我們就可以在整個桌子上看到一個很好的圓角。當該行被選中時,它會交換出一個漂亮的「突出顯示」單元格,以便用戶反饋他們已選擇一個單元格。
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
rowBackground = [UIImage imageNamed:@"topRow.png"];
selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:@"bottomRow.png"];
selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
}
else
{
rowBackground = [UIImage imageNamed:@"middleRow.png"];
selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
如果您希望只創建第一個單元格,即位於indexPath.row == 0的單元格,以使用自定義背景。
這是從馬特·加拉格爾的excellent site
派生你可以這樣做:
- (void)viewDidLoad {
[super viewDidLoad];
NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
[myTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
我添加了它,但它不起作用。 – 2011-05-07 18:12:08
這是因爲在tableview可以構建其單元格之前調用viewDidLoad。使用performSelector:onObject:afterDelay:在加載後調用它,或者嘗試viewDidAppear:如其他地方所建議的那樣。 – avance 2012-10-22 03:09:35
查看我上面的答案。從來沒有在viewDidLoad中使用它: 在viewDidAppear中調用此函數以獲得良好結果。 – 2012-10-31 11:40:40
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
在你的代碼,如果你想選擇在默認情況下的任何行用最好的方法,在viewDidAppear中使用。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear: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:0];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}
}
下面介紹如何在雨燕1.2做到這一點:
override func viewWillAppear(animated: Bool) {
let firstIndexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.selectRowAtIndexPath(firstIndexPath, animated: true, scrollPosition: .Top)
}
,只選擇第一個單元格的第一次正裝表,人們可能會認爲使用viewDidLoad
是正確的地方但是,在執行時,表沒有加載它的內容,所以它不會工作(並且可能會使應用程序崩潰,因爲NSIndexPath
將指向不存在的單元)。
解決方法是使用一個變量,該變量指示表之前已加載並相應地完成工作。
@implementation MyClass {
BOOL _tableHasBeenShownAtLeastOnce;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_tableHasBeenShownAtLeastOnce = NO; // Only on first run
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (! _tableHasBeenShownAtLeastOnce)
{
_tableHasBeenShownAtLeastOnce = YES;
BOOL animationEnabledForInitialFirstRowSelect = YES; // Whether to animate the selection of the first row or not... in viewDidAppear:, it should be YES (to "smooth" it). If you use this same technique in viewWillAppear: then "YES" has no point, since the view hasn't appeared yet.
NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection: 0];
[self.tableView selectRowAtIndexPath:indexPathForFirstRow animated:animationEnabledForInitialFirstRowSelect scrollPosition:UITableViewScrollPositionTop];
}
}
/* More Objective-C... */
@end
SWIT 3.0更新的解決方案
let indexPath = IndexPath(row: 0, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
這裏是我的SWIFT 3.0解決方案:
var selectedDefaultIndexPath = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if dataSource.isEmpty == false, selectedDefaultIndexPath == false {
let indexPath = IndexPath(row: 0, section: 0)
// if have not this, cell.backgroundView will nil.
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
// trigger delegate to do something.
_ = tableView.delegate?.tableView?(tableView, willSelectRowAt: indexPath)
selectedDefaultIndexPath = true
}
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
let cell = tableView.cellForRow(at: indexPath)
cell?.selectedBackgroundView?.backgroundColor = UIColor(hexString: "#F0F0F0")
return indexPath
}
我也試圖找出如何做到這一點。它需要在調用UITableViewDataSource函數之後進行設置,以便表視圖具有某些內容。 UITableView在加載數據後似乎沒有任何回調。 – jpsimons 2010-11-27 18:46:14