簡體   English   中英

UITableCellView中的UIButton圖像出現問題

[英]issue with UIButton image in UITableCellView

我正在制作一個簡單的計時器應用程序。 我有一個多行的uitable。 在該表中,我放置了一個UIButton。 一切正常,到目前為止效果很好,即我看到按鈕出現在每一行中。 接下來,我需要為UIButton設置一個圖像,例如,如果計時器正在運行,我想在UIButton中顯示stop_button_image,如果計時器已經啟動,我想將按鈕上的UIImage設置為start_button_image

問題是,如果刷新了表,那么我將獲得討厭的回溯,這取決於將UIButton setimage代碼放在哪里。 這是我的代碼供參考。

//WORKING CODE: (NOT THAT I WANT)
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //TESTING - Button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        // Try to retrieve from the table view a now-unused cell with the given identifier.
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // If no cell is available, create a new one using the given identifier.
        if (cell == nil)
        {
            // Use the default cell style.
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

            //TESTING - Button
            [button addTarget:self
                       action:@selector(timerStopStart:)
             forControlEvents:UIControlEventTouchDown];
            [button setTitle:@"Start/Stop" forState:UIControlStateNormal];
            button.frame = CGRectMake(5.0f, 5.0f, 72.0f, 77.0f);
            button.tag = indexPath.row;
            [cell addSubview:button];

    //THIS WORKS IF I PUT THIS HERE
            [button setImage:[UIImage imageNamed:@"button_pause.png"] forState:UIControlStateNormal];

        }
        else 
        {
             //NSLog(@"went here 2");
             button = (UIButton *) [cell viewWithTag:indexPath.row];
        }

//some other logic
    }

//此處有代碼中斷

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            //TESTING - Button
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

            // Try to retrieve from the table view a now-unused cell with the given identifier.
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

            // If no cell is available, create a new one using the given identifier.
            if (cell == nil)
            {
                // Use the default cell style.
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

                //TESTING - Button
                [button addTarget:self
                           action:@selector(timerStopStart:)
                 forControlEvents:UIControlEventTouchDown];
                [button setTitle:@"Start/Stop" forState:UIControlStateNormal];
                button.frame = CGRectMake(5.0f, 5.0f, 72.0f, 77.0f);
                button.tag = indexPath.row;
                [cell addSubview:button];


            }
            else 
            {
                 //NSLog(@"went here 2");
                 button = (UIButton *) [cell viewWithTag:indexPath.row];
            }

//CODE BREAKS HERE
[button setImage:[UIImage imageNamed:@"button_pause.png"] forState:UIControlStateNormal];

    //some other logic
        }

問題是,當刷新表時,它將使用代碼button =(UIButton *)[cell viewWithTag:indexPath.row];調用該部分。 我知道這只是參考。

這是我在iOS模擬器中遇到的錯誤。 我真的想將圖像代碼放在cell == null檢查之外。 任何線索如何做到這一點?

這是我在模擬器中遇到的錯誤

2012-06-14 12:39:27.246 Timers[5381:10a03] -[UITableViewCell setImage:forState:]: unrecognized selector sent to instance 0x73706e0
2012-06-14 12:39:27.247 Timers[5381:10a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setImage:forState:]: unrecognized selector sent to instance 0x73706e0'
*** First throw call stack:
(0x150b052 0x16bbd0a 0x150cced 0x1471f00 0x1471ce2 0x136c6 0x7b5e0f 0x7b6589 0x7a1dfd 0x7b0851 0x75b322 

問題可能出在indexPath.row為零時。 在這種情況下,您調用viewWithTag:0但是由於任何UIView的標簽默認情況下均為0,因此,很多單元格子視圖都有一個標簽值為0的標簽,而不僅僅是UIButton。

為避免此問題,請在將它影響到標記之前,先向其indexPath.row添加一個常量(例如100),以便第0行的按鈕具有標簽100而不是0(第1行的按鈕具有標簽) 101,依此類推...),避免將其與標簽為0的任何其他子視圖混淆。

是的,這將在第0行失敗。

button = (UIButton *) [cell viewWithTag:indexPath.row];

當indexPath.row為0時將返回單元格,因為cell.tag為0。


一般而言,此方法無效。 第一次重用單元格時,按鈕將帶有錯誤的標記。 例如,如果您重復使用第18行第2行中的單元格,則按鈕上的標簽將為2,但您要查找的標簽為18。它將返回nil

為什么不定義常量#define kMyStopStartButtonTag 12345並始終使用該常量。

if (cell == nil)
{
    …
    button.tag = kMyStopStartButtonTag;
    [cell addSubview:button];
    …
} else {
    button = (UIButton *) [cell viewWithTag:kMyStopStartButtonTag];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM