UITable View 셀에서 UIS 스위치
내장하려면 어떻게 해야 합니까?UISwitch
에서UITableView
셀? 예는 설정 메뉴에서 볼 수 있습니다.
현재 솔루션:
UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
cell.accessoryView = mySwitch;
일반적으로 액세서리 보기로 설정하는 것이 방법입니다.다음에서 설정할 수 있습니다.tableView:cellForRowAtIndexPath:
스위치를 뒤집을 때 타겟/액션을 사용하여 작업을 수행할 수 있습니다.이와 같습니다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch( [indexPath row] ) {
case MY_SWITCH_CELL: {
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease];
aCell.textLabel.text = @"I Have A Switch";
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
aCell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
return aCell;
}
break;
}
return nil;
}
- (void)switchChanged:(id)sender {
UISwitch *switchControl = sender;
NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}
UIS 스위치나 기타 컨트롤을 셀에 추가할 수 있습니다.accessoryView
. 그렇게 하면 세포의 오른쪽에 나타날 것이고 아마도 당신이 원하는 것일 것입니다.
if (indexPath.row == 0) {//If you want UISwitch on particular row
UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[cell addSubview:theSwitch];
cell.accessoryView = theSwitch;
}
Interfacebuilder에서 셀을 준비하고 View 컨트롤러의 IBOutlet에 연결한 후 테이블 뷰가 적절한 행을 요구할 때 반환할 수 있습니다.
대신, 셀에 대한 별도의 xib을 생성하고(다시 IB로) 셀 생성 시 UINib을 사용하여 로드할 수 있습니다.
마지막으로, 프로그래밍 방식으로 스위치를 생성하여 셀 컨텐츠 보기 또는 액세서리 보기에 추가할 수 있습니다.
어떤 것이 당신에게 가장 잘 어울리는지는 주로 당신이 무엇을 하고 싶은지에 달려 있습니다.테이블 보기 내용이 고정되어 있으면(설정 페이지 등에 대해) 처음 두 가지가 잘 작동할 수 있고, 내용이 동적이면 프로그래밍 방식의 솔루션이 더 좋습니다.무엇을 하고 싶은지 좀 더 구체적으로 말씀해 주세요. 그러면 질문에 대답하기가 쉬워질 것입니다.
뷰 계층(UITableViewCell)에서 꺼졌다 켜졌다가 하는 경우 보다 완벽한 솔루션으로 이벤트를 테이블뷰 위임자에게 전달합니다.didSelect
그리고.didDeselect
:
class CustomCell: UITableViewCell {
private lazy var switchControl: UISwitch = {
let s = UISwitch()
s.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
return s
}()
override func awakeFromNib() {
self.accessoryView = switchControl
self.selectionStyle = .none // to show the selection style only on the UISwitch
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
(self.accessoryView as? UISwitch)?.isOn = selected
}
@objc private func switchValueDidChange(_ sender: UISwitch) { // needed to treat switch changes as if the cell was selected/unselected
guard let tv = self.superview as? UITableView, let ip = tv.indexPath(for: self) else {
fatalError("Unable to cast self.superview as UITableView or get indexPath")
}
setSelected(sender.isOn, animated: true)
if sender.isOn {
tv.delegate?.tableView?(tv, didSelectRowAt: ip)
} else {
tv.delegate?.tableView?(tv, didDeselectRowAt: ip)
}
}
}
그리고 당신의 대리인에게.
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return false // to disable interaction since it happens on the switch
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // to make sure it is rendered correctly when dequeuing:
// stuff
if isSelected { // stored value to know if the switch is on or off
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
} else {
tableView.deselectRow(at: indexPath, animated: true)
}
// more stuff
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// do your thing when selecting
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
// do your thing when deselecting
}
신속한 이용을 위하여
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "TableIdentifer")
let aswitch = UISwitch()
cell.accessoryView = aswitch
}
언급URL : https://stackoverflow.com/questions/3770019/uiswitch-in-a-uitableview-cell
'sourcecode' 카테고리의 다른 글
MySQL 데이터베이스에 많은 양의 히트를 기록하는 모범 사례 (0) | 2023.09.20 |
---|---|
Oracle 오류 ORA-01790을 해결하는 방법은 무엇입니까? (0) | 2023.09.20 |
ui-grid 글꼴 파일을 프로젝트에 포함하는 방법 (0) | 2023.09.20 |
jquery 페이드 요소에 '가시성: 숨김' 스타일의 요소가 표시되지 않습니다. (0) | 2023.09.15 |
쿼리에서 계산된 열을 기준으로 그룹화하는 방법은 무엇입니까? (0) | 2023.09.15 |