2013-07-03 83 views
1

我想改變形象,當我點擊一個細胞,但它不工作圖像不會被調用的Objective-C

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
    UITableViewCell *cell = [feedsTableView cellForRowAtIndexPath:indexPath]; 

UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011]; 
UIImage *image = (UIImage *)[cell.contentView viewWithTag:0017]; 

image = [UIImage imageNamed:@"reload.png"]; 
title.text = @"test"; 

cellClicked = YES; 

[feedsTableView beginUpdates]; 
[feedsTableView endUpdates]; 

[feedsTableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 

標題但是變化。

我的圖像如何添加到表格中。

CGRect imageFrame = CGRectMake(0, 0, 120, 90); 
    customImage.tag = 0017; 
    self.customImage = [[UIImageView alloc] initWithFrame:imageFrame]; 
    [cell.contentView addSubview:self.customImage]; 

回答

2

你需要做這樣的事情:

UIImageView *imageView = [cell.contentView viewWithTag:0017]; 

imageView.image = [UIImage imageNamed:@"reload.png"]; 

UIImages不是UIView的子類。注意你的第二塊代碼是如何將UIImageView添加到單元格而不是UIImage。

2

您是否知道在C(以及Objective-C和C++)中,前導0的整數文字是八進制數,而不是十進制數。

因此:

0011 != 11 
0017 != 17 
+0

雖然這是真的,但只要您繼續使用'0011'作爲標籤並使用'0011'來獲得帶有該標籤的視圖,就不會產生影響。 – Jasarien

+0

@Jasarien IB是否遵循相同的約定(假設這是標籤設置的地方)? – trojanfoe

+0

這是另一個問題......它取決於IB是否將字段中的內容字面上看作整數,或者將其解析爲整數格式(從而剝離前導零)。將不得不測試。無論如何,這個問題表明標籤在代碼中被設置爲'0017'。 – Jasarien

0

而不是增加從表視圖的意見,爲什麼不直接使用一個自定義的UITableViewCell?

通過這種方式,您可以避免使用標籤,因爲您可以將標籤和圖像視圖設置爲單元格的屬性,從而避免了這個問題。

它的性能也稍高一些,因爲標籤和圖像視圖將與單元格一起重用,而不是每次都需要設置它們。

+0

我不需要用新的XIB替換單元格嗎?如果你知道一個好的教程,或者我可以開始的東西,將不勝感激。 – IamGretar

+0

如果你願意,你可以將你的元素放在代碼中。看看[自定義單元格](http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7 -SW18)部分。 – Abizern