programing

UITableView에서 셀을 선택할 수 없도록 만드는 방법은 무엇입니까?

bestprogram 2023. 8. 20. 12:20

UITableView에서 셀을 선택할 수 없도록 만드는 방법은 무엇입니까?

UITableView 상단에 삽입할 셀이 있습니다.사용자가 셀을 클릭할 때 파란색 선택 표시기가 표시되지 않도록 하려면 어떻게 해야 합니까?

행 색인에 따라 테이블 셀 또는 특정 테이블 셀을 선택하는 기능을 제거하려면 다음을 사용합니다.willSelectRowAt

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {


        return nil
    }

요소 선택의 UI 효과를 간단히 제거하려면 UITableViewCell의 선택 스타일을 다음과 같이 설정합니다.UITableViewCellSelectionStyleNone

스위프트 5:

selectionStyle = .none

셀 단위로 셀을 완전히 선택할 수 없게 하려면 다음 두 가지가 필요합니다.

1 - 다른 사람들이 말했듯이:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

2 - 다음과 같이 딜러 방법을 구현합니다.

// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.selectionStyle == UITableViewCellSelectionStyleNone){
        return nil;
    }
    return indexPath;
}

스토리보드에서 테이블 보기에 대해 다음을 설정합니다.

선택:선택 없음

터치선택 항목 표시: 거짓

enter image description here

할수있습니다

cell.selectionStyle = UITableViewCellSelectionStyleNone;

스위프트 3의 경우 사용할 수 있습니다.

 cell.isUserInteractionEnabled = false

빠른 구문:

cell.selectionStyle = UITableViewCellSelectionStyle.None

TableViewCell을 비활성화하는 관점에서 답변드립니다.스토리보드를 사용할 수 있습니다.

XCode Version 8.2.1 (8C1002)

스토리보드에서 TableVewCell을 선택하면 오른쪽 패널 - 유틸리티에 다음이 표시됩니다.

Utilities Sidebar

선택: 없음

그게 다야!

문제는 어떻게 특정 세포를 선택할 수 있게 하고 다른 세포들은 선택할 수 없게 하느냐였습니다.(다른 많은 제안을 시도한 후) 이것이 제 해결책이었습니다.

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if (indexPath.row == 1 || indexPath.row == 2) {
        return indexPath
    }
    return nil        
}
myTable.allowsSelection = false

Swift 3에서 업데이트:

cell.selectionStyle = UITableViewCellSelectionStyle.none

Swift 3의 경우:

cell.selectionStyle = .none

방법 구현UITableViewDelegate

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return NO;
}

스위프트 5

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell;
    cell.selectionStyle = .none
}

스위프트 5

당신도 이것을 시도해보고 싶을지도 모릅니다.

tableView.allowsSelection = false

언급URL : https://stackoverflow.com/questions/2641530/how-to-make-a-cell-on-a-uitableview-not-selectable