在我的UITableView
中,當它進入編輯模式時,我只想選擇一些選定的單元格。我知道UITableView
班有財產allowsSelectionDuringEditing
,但這適用於整個UITableView
。我沒有看到任何相關的委託方法在每個單元的基礎上進行設置。UITableView編輯時的單元格選擇模式
我能想出的最佳解決方案是將allowsSelectionDuringEditing
設置爲YES
。然後,在didSelectRowAtIndexPath
中,如果表視圖正在編輯,則過濾掉任何不需要的選擇。另外,在cellForRowAtIndexPath
中,將這些單元格selectionStyle
更改爲無。
問題是進入編輯模式不會重新加載UITableViewCells
,所以他們的selectionStyle
直到他們滾動離屏時纔會更改。所以,在setEditing中,我還必須迭代可見單元格並設置它們的selectionStyle
。
這有效,但我只是想知道是否有更好/更優雅的解決方案來解決這個問題。我的代碼的基本輪廓附加。任何建議非常感謝!謝謝。
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
if (self.editing && ![self _isUtilityRow:indexPath]) return;
// Otherwise, do the normal thing...
}
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// UITableViewCell* cell = ...
if (self.editing && ![self _isUtilityRow:indexPath])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
return cell;
}
- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing)
{
for (UITableViewCell* cell in [self.tableView visibleCells])
{
if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]])
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
}
else
{
for (UITableViewCell* cell in [self.tableView visibleCells])
{
if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]])
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
}
}
}
您可以在進入編輯模式時重新加載表格,或者在選擇該表格後立即取消選擇該行,而不是將選擇樣式設置爲無... – AMayes 2013-01-10 21:29:11