2016-12-27 73 views
1

我在iOS表格行中實現了行顏色更改邏輯,我通過將標題更改爲「更改顏色」來使用刪除按鈕,並且點擊那個按鈕,我想改變那個按鈕被點擊的特定行的顏色。如何在點擊刪除按鈕上更改表格行顏色,而不是刪除iOS中的一行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
if (editingStyle == UITableViewCellEditingStyleDelete) 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.contentView.backgroundColor = [UIColor redColor]; 
} 
} 

我試圖執行顏色變化邏輯內部上面的方法,但它不是反射,並且只有數據重裝後反射,但仍索引改變-2行。

如何更改點擊「更改顏色(刪除)」按鈕並隱藏按鈕(即取消編輯)的行的表顏色?

回答

1

你幾乎擁有合適的解決方案,你需要做的一切,就是:

  1. 查找使用indexPath TableView中右邊的單元格。
  2. 更改所選單元格的背景顏色。
  3. 只刷新所選indexPath的tableView。

    - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // 1. Find cell UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; // 2. Change bg colour selectedCell.contentView.backgroundColor = [UIColor redColor]; // 3. Refresh tableView [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade]; } }

Here是一個示例的viewController,它應該如何被使用。

+0

謝謝你的工作完美 – Kiran

0

我不是iOS的親,所以請原諒我的無知,但是是一個本地刪除按鈕?我還沒有看到呢,所以就不會知道..

但是如果我實現這個,我會在你的文件的頂部定義常量:#define kCellTagDelete = 99(或其他一些未使用的標籤),

然後,按下刪除按鈕後,更改單元格的標記kCellTagDelete,然後用正確的indexPath(可以使用大小爲1的數組)調用reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation,並更新cellForRowAtIndexPath:方法以檢查標記== kCellTagDelete,如果是這樣,格式適當。

+0

嗨,刪除按鈕是在編輯行中的表格上,請你幫忙 – Kiran

相關問題