2014-02-12 25 views
2

我有一個應用程序,其中有一個UITableView填充的UIButtons以編程方式創建。下面的代碼顯示瞭如何創建一個UIButton在模擬器上工作但不在設備上的UIButton圖像

它在模擬器上完美工作,但不在設備上。在模擬器上,按鈕顯示爲buttonImage.png,但在設備上圖像不存在。按鈕的所有其他方面都是正確的。

就我所知,圖像已被正確添加到資源中。有任何想法嗎?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
if (cell ==nil) 
{ 
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setFrame:CGRectMake(0,0,160, 160)]; 
[button setBackgroundImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal]; 
[button setTitleColor:[UIColor colorWithRed:19/255.0f green:73/255.0f blue:17/255.0f alpha:1] forState:UIControlStateNormal]; 
button.titleLabel.font = [UIFont fontWithName:@"arial" size:20]; 
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown]; 
[button setTitle:@"button" forState:UIControlStateNormal]; 

//more buttons created as needed 

NSMutableArray *buttonArray = [[NSMutableArray alloc] init]; 
[array addObject:button]; 
//more buttons added to array as needed 

[cell addSubview:[buttonArray objectAtIndex:indexPath.row]]; 

return cell; 

}

+0

嘗試用戶交互'[cell.contentview addSubview:[buttonArray objectAtIndex:indexPath.row];' –

+1

嘿!你必須檢查一個愚蠢的事情可能是這會是你的問題。檢查名稱的情況應該是相同的。例如: - buttonImage或buttonimage它應該是相同的名稱相同case.Otherwise它會顯示它在模擬器不在設備上。 – ManiaChamp

+0

這可能是在您的代碼問題:http://stackoverflow.com/a/6773296/817365 –

回答

11

檢查image nam e,因爲模擬器不區分大小寫,但設備區分大小寫。

+0

如果它的工作,然後接受我的回答 – Manimaran

+0

我儘快讓我:) :) – RGriffiths

0

儘量使你的代碼如下更改:

變化control event

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchInside]; 

添加按鈕細胞的內容視圖

[cell.contentview addSubview:[buttonArray objectAtIndex:indexPath.row]]; 
0

嘗試從設備中刪除已安裝的應用程序,然後再次運行它

+0

一個大寫字母是問題 - 看到答覆的答案。謝謝你的幫助。 – RGriffiths

1

您的代碼沒有出錯。我相信你已經測試了你的應用程序在非視網膜模擬器&它顯示按鈕。

所以現在當你在設備上測試它時,我確定你正在測試視網膜設備&你還沒有提供你的按鈕圖像的2x版本。

解決方案::添加一個2x版本的圖像到您的資源。

例:: yourImage.png(32×32),然後添加其他圖片與名字 [email protected](64×64)

這解決您的問題的資源。

希望有所幫助。

+0

一個大寫字母是問題 - 請參閱打勾的答案。謝謝你的幫助。 – RGriffiths

+0

這個解決方案對我的蘋果手錶應用程序很有幫助。 – alp

2

我在使用XCode導航器窗口重命名應用程序中的圖標時遇到了類似的問題。在模擬器中一切都很好,但是當我(最終)在我的設備上進行測試時,圖標不會顯示出來。我驗證了拼寫和案例,無數次(在閱讀了上述接受的答案之後),我甚至將圖標名稱複製/粘貼到......沒有骰子。

我試着清理應用程序並重建...沒有骰子。

最後,我完全關閉了XCode,並從'Launchpad'重新啓動,並且它工作正常。

這是一個令人沮喪的時刻。感覺對我來說,就像在那裏有一個錯誤。

+0

檢查拼寫後(如接受的答案中所建議的),我嘗試了這個建議。在重新啓動XCode之後,我也做了一次清理/構建,這是很好的措施。它在那之後工作。感謝提示。爲我節省了數小時的調試時間。 – chr

+0

注意:直到您執行「產品 - >清理」才能強制編譯器在您下次構建時重新生成資產目錄時,更改標題不起作用。 –

0

檢查拼寫,也允許button.allowUserInteraction = true

相關問題