【发布时间】:2021-10-29 12:13:39
【问题描述】:
我的 tableview 上有某些单元格/行在打开视图时应该设置为“已选择”。在我下面的代码中,如果数据源包含特定的用户 ID,则应出现绿色复选标记(此部分有效),并且该行应为“已选择”。也就是说,选定的状态似乎不起作用。如何将单元格设置为选中?
ViewController.m
-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
NSDictionary *client = self.sectionClients[indexPath.section][indexPath.row];
static NSString *ClientTableIdentifier = @"ClientTableViewCell";
ClientTableViewCell *cell = (ClientTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:ClientTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ClientTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *photo = client[@"client photo"];
cell.clientName.text = [NSString stringWithFormat:@"%@ %@", client[@"first name"], client[@"last name"]];
cell.subtext.text = client[@"phone"];
NSURL *imageUrl = [NSURL URLWithString:photo];
[cell.clientPhoto setImageWithURL:imageUrl];
if (self.selectionData != NULL && [[self.selectionData valueForKey:@"clients ids"] containsString:client[@"nid"]])
{
cell.greenCheck.image = [UIImage imageNamed:@"added.png"];
[cell setSelected:YES];
}
return cell;
}
【问题讨论】:
-
根据我的经验,向单元格添加任何最后一分钟的视觉更改的最佳位置是
willDisplayCell。
标签: ios objective-c uitableview