2013-05-14 57 views
0

我正在使用UITableView顯示多行數據:textLabel.text名稱,imageView顯示狀態(在線/離線),detailTextLabel其他信息。更新UITableView的imageView/detailTextLabel

ex。在的cellForRowAtIndexPath

cell.imageView.image = [UIImage imageNamed:@"checking.png"]; 
cell.textLabel.text = @"NAME"; 
cell.detailTextLabel.text = @"Checking..."; 

如何更新從另一個功能imageView.image和detailTextLabel.text。有沒有UITableView方法? 我猜我需要使用IndexPath行來定位單元格。也許爲每個imageView和detailTextLabel分配標籤?

謝謝。

回答

0

您更好的選擇是更新模型中的數據,然後要求表格視圖爲reloadData(或者,如果您明確知道哪些行已更新,reloadRowsAtIndexPaths:withRowAnimation:)。

這保持了您的數據模型的完整性,並且是管理視圖最簡單的代碼。

+0

但是,如何定位行(IndexPath.row)並更新detailTextLabel和imageView.image?你能提供一些示例代碼嗎?謝謝。 – ialphan 2013-05-14 16:00:57

+0

您正在實施表視圖數據源方法嗎? tableView:cellForRowAtIndexPath: – Wain 2013-05-14 17:05:31

+0

是的,我結束了使用這樣的事情:UITableViewCell * cell0 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];其中indexPathForRow和inSection是固定的。 Frm,我能夠像這樣更新cell0 cell0.imageView.image = [UIImage imageNamed:@「connected.png」]; – ialphan 2013-05-14 22:14:18

0

你應該嘗試使用性質是這樣的:

@property (nonatomic, strong) UIImage *image; 
@property (nonatomic, strong) NSString *textLabel; 
@property (nonatomic, strong) NSString *detailTextLabel; 

然後你,你這樣做的屬性添加到您的細胞:

cell.imageView.image = image; 
cell.textLabel.text = textLabel; 
cell.detailTextLabel.text = detailTextLabel; 

每當更新數據時,可以調用方法,重新加載您的單元格(包含要重新加載的單元格的索引路徑的數組):

[self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone]; 
+0

也不能正常工作:[self.tableView beginUpdates]; \t detailTextLabel1 = @「新的細節文本」; \t [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; \t [self.tableView endUpdates]; \t [self.tableView reloadData]; – ialphan 2013-05-14 16:40:07