[英]How to deselect a selected UITableView cell?
我正在處理一個必須預先選擇特定單元格的項目。
我可以使用-willDisplayCell
預選一個單元格,但是當用戶單擊任何其他單元格時我無法取消選擇它。
- (void)tableView:(UITableView*)tableView
willDisplayCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath
{
AppDelegate_iPad *appDelegte =
(AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
if ([appDelegte.indexPathDelegate row] == [indexPath row])
{
[cell setSelected:YES];
}
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AppDelegate_iPad *appDelegte =
(AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
NSIndexPath *indexpath1 = appDelegte.indexPathDelegate;
appDelegte.indexPathDelegate = indexPath;
[materialTable deselectRowAtIndexPath:indexpath1 animated:NO];
}
你能幫我嗎?
使用此代碼
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Change the selected background view of the cell.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Change the selected background view of the cell.
tableView.deselectRow(at: indexPath, animated: true)
}
在表視圖委托的didSelectRowAtIndexpath
使用以下方法(或從任何地方)
[myTable deselectRowAtIndexPath:[myTable indexPathForSelectedRow] animated:YES];
嘗試這個:
for (NSIndexPath *indexPath in tableView.indexPathsForSelectedRows) {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
為此在 Swift 中進行擴展可能會很有用。
斯威夫特 4 和斯威夫特 5:
Swift 擴展(例如在 UITableViewExtension.swift 文件中):
import UIKit
extension UITableView {
func deselectSelectedRow(animated: Bool)
{
if let indexPathForSelectedRow = self.indexPathForSelectedRow {
self.deselectRow(at: indexPathForSelectedRow, animated: animated)
}
}
}
使用例如:
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.tableView.deselectSelectedRow(animated: true)
}
請檢查委托方法是否正確。 例如;
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
為了
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
斯威夫特 4:
tableView.deselectRow(at: indexPath, animated: true)
這是 Swift 4 的解決方案
in tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
只需添加
tableView.deselectRow(at: indexPath, animated: true)
它就像一個魅力
例子:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
//some code
}
除了將單元格設置為選中狀態之外,還需要通知tableView該單元格被選中。 將-tableView:selectRowAtIndexPath:animated:scrollPosition:
的調用添加到您的willDisplayCell:
方法: 中,您將能夠像往常一樣取消選擇它。
- (void)tableView:(UITableView*)tableView
willDisplayCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath
{
AppDelegate_iPad *appDelegte = (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
if ([appDelegte.indexPathDelegate row] == [indexPath row])
{
// SELECT CELL
[cell setSelected:YES];
// TELL TABLEVIEW THAT ROW IS SELECTED
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
一定要使用 UITableViewScrollPositionNone 來避免奇怪的滾動行為。
基於 saikirans 解決方案,我寫了這個,這對我有幫助。 在 .m 文件上:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
selectedRowIndex = nil;
}
else { self.selectedRowIndex = [indexPath retain]; }
[tableView beginUpdates];
[tableView endUpdates];
}
在頭文件上:
@property (retain, nonatomic) NSIndexPath* selectedRowIndex;
我也不是很有經驗,所以仔細檢查內存泄漏等。
如果您想通過第一次單擊選擇任何表格單元格並在第二次單擊時取消選擇,您應該使用以下代碼:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedIndexPath &&
[indexPath compare:self.selectedIndexPath] == NSOrderedSame) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.selectedIndexPath = nil;
}
else {
self.selectedIndexPath = indexPath;
}
}
這應該有效:
[tableView deselectRowAtIndexPath:indexpath1 animated:NO];
只要確保 materialTable 和 tableView 指向同一個對象。
材質是否連接到 Interface Builder 中的 tableView?
斯威夫特 4:
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
let cell = tableView.cellForRow(at: indexPath)
if cell?.isSelected == true { // check if cell has been previously selected
tableView.deselectRow(at: indexPath, animated: true)
return nil
} else {
return indexPath
}
}
斯威夫特 2.0:
tableView.deselectRowAtIndexPath(indexPath, animated: true)
嘗試這個
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
NSIndexPath * index = [self.menuTableView indexPathForSelectedRow];
[self.menuTableView deselectRowAtIndexPath:index animated:NO];
根據您的代碼放置此代碼,您將取消選擇您的單元格。
你能試試這個嗎?
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AppDelegate_iPad *appDelegte =
(AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
NSIndexPath *indexpath1 = appDelegte.indexPathDelegate;
appDelegte.indexPathDelegate = indexPath;
UITableViewCell *prevSelectedCell = [tableView cellForRowAtIndexPath: indexpath1];
if([prevSelectedCell isSelected]){
[prevSelectedCell setSelected:NO];
}
}
它對我有用。
斯威夫特 3.0:
遵循ray wenderlich swift style guide的協議一致性部分,為了將相關方法組合在一起,將此擴展放在您的視圖控制器類下方,如下所示:
// your view controller
class MyViewcontroller: UIViewController {
// class stuff here
}
// MARK: - UITableViewDelegate
extension MyViewcontroller: UITableViewDelegate {
// use the UITableViewDelegate method tableView(_:didSelectRowAt:)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// your code when user select row at indexPath
// at the end use deselectRow
tableView.deselectRow(at: indexPath, animated: true)
}
}
斯威夫特 3/4
在視圖控制器中:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
在自定義單元格中:
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = .none
}
迅速
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// your code
// your code
// and use deselect row to end line of this function
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
斯威夫特 3
我也遇到過這種情況 - 當您導航或展開回到表格視圖時,它通常不會為您取消選擇先前選擇的行。 我將此代碼放在表視圖控制器中,它運行良好:
override func viewDidAppear(_ animated: Bool) {
if let lastSelectedRow = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: lastSelectedRow, animated: true)
}
}
迅捷 3.0
tableView.deselectRow(at: indexPath, animated: true)
由於預加載的數據,在第一次點擊時取消選擇(DidDeselectRowAt); 您需要通知 tableView 該行已經被選中,以便初始單擊然后取消選擇該行:
//斯威夫特3:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView[indexPath.row] == "data value"
tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
}
}
我正在做一個必須預先選擇特定單元格的項目。
我可以使用-willDisplayCell
來-willDisplayCell
選擇一個單元格,但是當用戶單擊任何其他單元格時, -willDisplayCell
選擇它。
- (void)tableView:(UITableView*)tableView
willDisplayCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath
{
AppDelegate_iPad *appDelegte =
(AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
if ([appDelegte.indexPathDelegate row] == [indexPath row])
{
[cell setSelected:YES];
}
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AppDelegate_iPad *appDelegte =
(AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
NSIndexPath *indexpath1 = appDelegte.indexPathDelegate;
appDelegte.indexPathDelegate = indexPath;
[materialTable deselectRowAtIndexPath:indexpath1 animated:NO];
}
你能幫我嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.