UITableView 일부 셀을 "선택 불가능"으로 설정
UITableView의 셀 속성을 선택 불가능하게 설정하려면 어떻게해야합니까? 사용자가 셀을 누를 때 파란색 선택 상자를보고 싶지 않습니다.
테이블 셀의 selectionStyle
속성을로 설정하십시오 UITableViewCellSelectionStyleNone
. 그러면 강조 표시되지 않으며에서 해당 속성을 확인할 수도 있습니다 tableView:didSelectRowAtIndexPath:
.
행 선택을 방지하려면
완전히의 선택을 방지하기 위해 UITableViewCell
, 당신은이 UITableViewDelegate
구현 tableView:willSelectRowAtIndexPath:
. 해당 메소드 nil
에서 행을 선택하지 않으려면 리턴 할 수 있습니다 .
- (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)path
{
// Determine if row is selectable based on the NSIndexPath.
if (rowIsSelectable) {
return path;
}
return nil;
}
이렇게하면 행이 선택 tableView:didSelectRowAtIndexPath:
되고 호출되지 않습니다. 그러나 이렇게해도 행이 강조 표시되는 것은 아닙니다 .
행 강조 표시를 방지하려면
터치시 행이 시각적으로 강조 표시되지 않게하려면 셀이 selectionStyle
로 설정되어 있는지 확인 UITableViewCellSelectionStyleNone
하거나 바람직하게 는 다음과 같이 UITableViewDelegate
구현할 수 있습니다 tableView:shouldHighlightRowAtIndexPath:
.
- (BOOL)tableView:(UITableView *)tv shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
// Determine if row is selectable based on the NSIndexPath.
return rowIsSelectable;
}
이것을 사용하십시오 :
cell.selectionStyle = UITableViewCellSelectionStyleNone;
iOS 6 이상에만 해당합니다.
tableView:shouldHighlightRowAtIndexPath:
델리게이트 에서 메소드 를 구현할 수 있습니다 . 자세한 내용은 여기를 참조하십시오 : http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
이 문제도 이미 언급 한 모든 것을 시도했습니다. 표 셀을 선택할 때 "파란색 플래시"를 제거한 마지막 요령은 다음 줄을 추가하는 것입니다.
self.myTableView.allowsSelection = NO;
이것이 한 줄인지 또는 모든 것이 결합되었는지 확실하지 않지만 총 결과로 더 이상 파란색 선택이나 파란색 플래시가 표시되지 않습니다. 행복!
세트 cell.userInteractionEnabled = NO;
IB를 사용하는 것도 우아한 방법입니다.
Apple은 didSelectRowAtIndexPath에서 가장 먼저해야 할 일은 행을 선택 취소하는 것이라고 말합니다.
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
그런 다음 AccessoryType을 확인 표시 또는 없음 등으로 변경할 수 있습니다. 따라서 didSelectRowAtIndexPath를 입력하면 행을 선택 취소 할 수 있으며, 선택하지 않을 경우 해당 행을 확인하지 마십시오.
다른 방법은에 몇 가지 범주 방법을 추가하는 것 UITableViewCell
입니다. 나는 테이블을 만드는 방식 때문에 Sebastians (또한 좋은) 대답보다 이것을 좋아합니다. 다른 사람에게 도움이 될 것이라고 생각했습니다.
- (void)setSelectable:(BOOL)enabled {
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
[self setUserInteractionEnabled:enabled];
}
- (BOOL)isSelectable {
BOOL disabled = [self selectionStyle]==UITableViewCellSelectionStyleNone &&
[self isUserInteractionEnabled];
return ! disabled;
}
Swift 4:
You can prevent selection and highlighting by using the UITableViewDelegate: shouldHighlightRowAt
This answer assumes you have a custom cell created named: CustomTableViewCell
And that you created a boolean inside that custom cell named: isSelectable
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
if cell.isSelectable == false {
return false
} else {
return true
}
}
This is the Swift version of Sebastian Celis's objc answer.
If you have designed your cell in Interface Builder, you can do this by removing the checkbox from 'User Interaction Enabled' for the tableViewCell
.
There is another simple way to avoid the selection appearing as blue.
Select a cell you don't want to appear as blue.
Then select the attributes inspector (the shield icon next to the ruler icon on the properties view on the side).
Then change the 'Selection' field from 'Blue' to 'None'.
Note, presumably this is still selecting, it will just not appear as selected if all you want is to avoid the UI effect.
Use tableView: willDisplayCell: forRowAtIndexPath: instead of tableView: didSelectRowAtIndexPath: to get rid of the flash that appears first time you touch the cell.
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
To make certain row unselected you have to make some changes in two methods of UITableView delegate.
In the method below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
after allocation of the cell, write the code below
if(indexPath.row == someCellNumber) cell.selectionStyle =UITableViewCellSelectionStyleNone;
The above code will prevent highlighting the cell, if somehow user tries to selects.
in this delegate method below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == someCellNumber) return;
//for other cells write the code below...
}
if you don't write if(indexPath.row == someCellNumber) return;
row will still be selected and there is a chance of app crash
Swift 4:
In the cell class:
selectionStyle = .none
참고 URL : https://stackoverflow.com/questions/812426/uitableview-setting-some-cells-as-unselectable
'Programming' 카테고리의 다른 글
Swift에서 명령 행 인수에 어떻게 액세스합니까? (0) | 2020.08.04 |
---|---|
SQLAlchemy 행 항목을 업데이트하는 방법? (0) | 2020.08.04 |
비밀번호를 해시하는 방법 (0) | 2020.08.04 |
Java Long 기본 유형 최대 한계 (0) | 2020.08.04 |
새 탭의 javascript window.location (0) | 2020.08.04 |