2011-03-28 60 views
0

我有一個自定義的UIButton我TableViewCell通過:自定義的UIButton中的UITableViewCell - 刪除不工作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *ident = @"indet"; 

    cell = [tableView dequeueReusableCellWithIdentifier:ident]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease]; 

    } 

    button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setFrame: CGRectMake(230.0f, 7.5f, 43.0f, 43.0f)]; 
    [button setImage:[UIImage imageNamed:@"check_bak.png"] forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(removeEntry) forControlEvents:UIControlEventTouchUpInside]; 
    button.tag = [indexPath row]; 
    [cell addSubview:button]; 
    cell.textLabel.text = [myArray objectAtIndex:indexPath.row]; 
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:20.0]; 
    cell.textLabel.shadowColor = [UIColor whiteColor]; 
    cell.textLabel.shadowOffset = CGSizeMake(0,1); 

    return cell; 
} 

我宣佈我的.h爲removeEntry功能indexPath。

removeEntry:

[myArray removeObjectAtIndex:indexPath.row]; 
[myTable reloadData]; 

myArray的是一個的NSMutableArray。

這種方式無法正常工作。

每次我在indexPath.row刪除一個條目,它都會刪除一個條目。但是錯誤的。它總是一個條目上面被刪除。即使我做indexPath.row + 1/-1。

還有別的辦法嗎?按鈕應該留在單元格中。

我希望這是可以理解的,對不起,我是德國人。 :)

+0

你如何將'indexPath'傳遞給你的'removeEntry'方法,以及你如何調用它? – benwong 2011-03-28 22:55:32

回答

0

您的表視圖中有多少節?在您的removeEntry方法中將indexPath.row的值輸出到控制檯可能是一個好主意。如果您點擊第一行,是否打印出0?第五行,正在打印出4等。

編輯:看你的代碼,你存儲的indexpath作爲按鈕的標記。在這種情況下,改變你的removeEntry方法,看起來像這樣(你可以改變你返回類型爲任何你想要返回):

- (void)removeEntry:(id)sender { 

和「removeEntry」後添加一個冒號,當你將你的目標:

[button addTarget:self action:@selector(removeEntry:) forControlEvents:UIControlEventTouchUpInside]; 

現在,裏面removeEntry,你可以這樣做:

UIButton *button = (UIButton *)sender; 
[myArray removeObjectAtIndex:button.tag]; 
[myTable reloadData]; 
+0

嗯,它總是不同,因爲我數w /「返回[myArray計數];」 – Nicolai 2011-03-28 22:44:02

+0

如果你要添加NSLog(@「%d」,indexPath.row);到removeEntry方法的開始,它應該顯示你點擊的地方的索引。您首先要確保索引對於您點擊的行是正確的。索引應始終爲1減去您點擊的行。 – NRaf 2011-03-28 22:50:26

+0

它總是0. :( – Nicolai 2011-03-28 23:10:15

0

使用下面的代碼和平

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *ident = @"indet"; 

    cell = [tableView dequeueReusableCellWithIdentifier:ident]; 
    if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease]; 

    } 
    if ([cell.contentView subviews]){ 
     for (UIView *subview in [cell.contentView subviews]) { 
       [subview removeFromSuperview]; 
      } 
    } 
    //below here your piece of code. 
} 

原因在於,在該方法中,我們重用了單元格,該單元格保留添加到單元格的所有子視圖,並僅刷新單元格的文本部分。

希望這會對你有用!

0

我已經在我的iOS與編碼的UITableView遇到同樣的問題。 我已經通過在cell.contentView上添加按鈕並從cell.contentView中刪除它來修復它。

[cell.contentView addSubview:button]; 

代替[細胞addSubview:按鈕]添加按鈕;

在將視圖添加到單元格以將單元格中的按鈕刪除之前,將以下代碼行添加到cellForRowIndexPath()方法中。

for(UIView *subview in [cell.contentView subviews]){ 
    [subview removeFromSuperView]; 
} 

它會正常工作。

相關問題