2010-04-28 38 views
54

Tôi có một ứng dụng được xem và tôi đang thêm một chế độ xem bảng dưới dạng chế độ xem con vào chế độ xem chính. Tôi đã lấy UITableViewDelegate để trả lời các phương thức bảng. Mọi thứ đều hoạt động tốt, nhưng tôi muốn chọn hàng đầu tiên hoặc UITableView làm mặc định được chọn (Được đánh dấu).Chọn Hàng đầu tiên làm mặc định trong UITableView

Vui lòng giúp tôi, với mã nào tôi cần và nơi tôi cần đặt.

+0

Tôi cũng đang cố gắng để tìm ra cách để làm điều này. Nó cần phải được thiết lập sau khi các hàm UITableViewDataSource đã được gọi để xem bảng có một cái gì đó trong đó. UITableView dường như không có bất kỳ cuộc gọi lại nào kích hoạt sau khi dữ liệu được tải. – jpsimons

Trả lời

0

Chúng tôi sử dụng hình nền tùy chỉnh cho ô dựa trên đó có phải là ô đầu tiên ... ô giữa hay ô cuối cùng. Bằng cách đó chúng tôi có được một góc nhìn tròn đẹp mắt cho toàn bộ bảng. Khi hàng được chọn, nó sẽ hoán đổi một ô 'được đánh dấu' đẹp để cung cấp cho người dùng nguồn cấp dữ liệu mà họ đã chọn một ô.

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; 

Nếu bạn chỉ muốn tạo ô đầu tiên, tại indexPath.row == 0, để sử dụng nền tùy chỉnh.

này có nguồn gốc từ Matt Gallagher của excellent site

-1

Bạn có thể làm như thế này:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0]; 
    [myTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom]; 
} 
+0

Tôi đã thêm nó, nhưng nó không hoạt động. –

+0

Đó là vì viewDidLoad được gọi trước khi tableview có thể xây dựng các ô của nó. Sử dụng performSelector: onObject: afterDelay: để gọi nó sau khi nó tải hoặc thử trên viewDidAppear: như được đề xuất ở nơi khác. – avance

+1

kiểm tra câu trả lời của tôi ở trên. không bao giờ sử dụng điều này trong viewDidLoad: gọi hàm này trong viewDidAppear để có kết quả tốt. –

105
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]; 
    [myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom]; 
} 

Cách tốt nhất để sử dụng điều này trong mã của bạn, nếu bạn muốn chọn bất kỳ hàng theo mặc định , sử dụng trong viewDidAppear.

+4

Ô của tôi sẽ không chọn trong viewDidAppear vì một số lý do lạ, nhưng nó hoạt động tốt trong viewWillAppear – RyanG

+1

Ứng dụng có thể gặp sự cố nếu có một số bộ lọc xảy ra và kết quả là mảng trống. Cần phải thêm một kiểm tra lúc bắt đầu –

+0

Tôi đang sử dụng phương pháp này để chọn một ô trên vị trí đầu tiên, sau đó tôi gọi đại biểu, nhưng ô được chọn bằng selectRowAtIndexPath sẽ không bao giờ bị bỏ chọn. Không cho phép MultipleSelection. Bất kỳ giúp đỡ? xin vui lòng, cảm ơn. – Frade

8
- (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]; 
    } 
5
- (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]; 
    } 
} 
3

Dưới đây là làm thế nào để làm điều này trong Swift 1.2:

override func viewWillAppear(animated: Bool) { 
    let firstIndexPath = NSIndexPath(forRow: 0, inSection: 0) 
    self.tableView.selectRowAtIndexPath(firstIndexPath, animated: true, scrollPosition: .Top) 
} 
1

Để chỉ chọn ô đầu tiên lần đầu tiên bảng được nạp, người ta sẽ nghĩ rằng việc sử dụng viewDidLoad là đúng nơi để đi, nhưng, tại thời điểm thực thi đó, bảng không tải nội dung của nó, vì vậy nó sẽ không hoạt động (và có thể làm hỏng ứng dụng vì NSIndexPath sẽ trỏ đến ô không tồn tại).

Giải pháp thay thế là sử dụng biến cho biết bảng đã được tải trước và thực hiện công việc tương ứng.

@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 
9

Swit 3.0 cập nhật Solution

let indexPath = IndexPath(row: 0, section: 0) 
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom) 
1

Đây là giải pháp của tôi cho nhanh 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 
} 
Các vấn đề liên quan