[英]How to change a UITableViewCell image when the cell is selected?
問題總結起來-我試圖讓有人單擊UITableViewCell時選中一個框,這涉及更改單元格中的圖像。 該表設置良好,工作正常-它正在顯示我想要的信息,並且單元格按應有的方式進行選擇。 但是,當調用didSelectRowAtIndexPath方法時,我無法更改圖像。
到目前為止,這就是我所擁有的。 我已經從其他表等中刪除了多余的代碼,但這應該是所有相關的東西。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == diabetesVisitTable) {
if (indexPath.section==0) {
cell.textLabel.text = [_microvascularValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Family Med Unchecked Box.jpg"];
}
else if (indexPath.section==1) {
cell.textLabel.text = [_macrovascularValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Family Med Unchecked Box.jpg"];
}
else if (indexPath.section==2) {
cell.textLabel.text = [_bloodSugarValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Family Med Unchecked Box.jpg"];
}
cell.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
self.diabetesVisitTable.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
return cell;
}
else return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.imageView.image = [UIImage imageNamed:@"Family Med Checked Box.jpg"];
}
在tableView:didSelectRowAtIndexPath:
實現中,您要向表視圖詢問要使用dequeueReusableCellWithIdentifier:
配置的新單元格。 您想要獲取用於該indexPath
的實際單元格,以便可以直接更新它:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"Family Med Checked Box.jpg"];
}
您還需要以某種方式更新表視圖的數據源,以記錄選擇了某行的事實,並且如果數據源指示已選擇該行,則更新tableView:cellForRowAtIndexPath:
以設置選中的圖像。 如果不執行這些步驟,則刷新時該單元將顯示為未選中狀態。
您正在嘗試更新didSelectRowAtIndexPath函數中的單元格,該函數返回void。 因此它不會更新tableview。
您應該從diddSelectRowAtIndexPath中獲取indexpath.row值,並將其傳遞給cellForRowAtIndexPath。 如果它們匹配,則更新單元格,它將起作用。
還要確保添加一個beginUpdate和endUpdate以反映重新加載表值
UITableViewDelegate有3種方法來處理單元格突出顯示:
- tableView:shouldHighlightRowAtIndexPath:
- tableView:didHighlightRowAtIndexPath:
- tableView:didUnhighlightRowAtIndexPath:
嘗試這樣:
- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
return YES;
}
- (void)tableView:(UITableView*)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"Highlightimage"];
}
- (void)tableView:(UITableView*)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"image"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.