我正在做一个项目,我必须预先选择一个特定的单元格。
我可以使用 -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];
}
斯威夫特 3.0:
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];
indexPathForSelectedRow
是一个方法而不是一个属性。
[myTable indexPathForSelectedRow]
。我之前在循环细胞。谢谢!
为此在 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)
}
viewWillAppear()
方法中有助于用户了解他从哪里回来。
尝试这个:
for (NSIndexPath *indexPath in tableView.indexPathsForSelectedRows) {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
请检查委托方法是否正确。例如;
-(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 以避免奇怪的滚动行为。
[cell setSelected:YES]
:selectRowAtIndexPath
将处理单元格的 selected
状态
基于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;
我也不是很有经验,所以仔细检查内存泄漏等。
这应该有效:
[tableView deselectRowAtIndexPath:indexpath1 animated:NO];
只要确保 materialTable 和 tableView 指向同一个对象。
材质是否连接到 Interface Builder 中的 tableView?
如果您想在第一次单击时选择任何表格单元格并在第二次单击时取消选择,您应该使用以下代码:
- (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;
}
}
斯威夫特 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
}
}
tableView.deselectRow...
之后添加 self.tableView(tableView, didDeselectRowAt: indexPath)
,因为它没有被自动调用
斯威夫特 2.0:
tableView.deselectRowAtIndexPath(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
}
尝试这个
[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];
}
}
它对我有用。
迅速
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)
}
斯威夫特 3.0:
在 protocol conformance section of the 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)
}
}
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)
}
}
不定期副业成功案例分享
willSelectRowAt:
内执行此操作更合适。在此处查看更完整的答案:stackoverflow.com/questions/12693402/…