我正在从plist文件加载tableView.这没有问题.我只是想"勾选"选定的行.目前,我的代码没有按预期工作.目前,它看起来如下:
我的代码:
class portals: UITableViewController {
var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0)
...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
cell.textLabel!.text = portals[indexPath.row]
return cell
}
// Check which portal is selected
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var whichPortalIsSelected: String = ""
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow();
// Tick the selected row
if indexPath!.row != …Run Code Online (Sandbox Code Playgroud) 我在滚动表时表格视图单元格没有保持"选中"状态时遇到问题.这是相关代码:
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndexPath = indexPath;
//do other stuff
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell_iPhone* cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell_iPhone"];
if (cell == nil)
cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell_iPhone" owner:self options:nil] objectAtIndex:0];
if ([indexPath compare: self.selectedIndexPath] == NSOrderedSame) {
[cell setSelected:YES animated:NO];
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
对于细胞:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
self.selectedBg.hidden = NO;
}else{
self.selectedBg.hidden = YES;
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{ …Run Code Online (Sandbox Code Playgroud) 早上好
我的问题是,当我向下和向上滚动时,我的表视图重用所选单元格.我的意思是当我从上向下选择一个单元格然后向下滚动时,我选择的一些单元格被选中,也有一些选中的单元格是当我再次向上滚动时没有显示选中,当发生这种情况时我再次选择苹果不仅仅是一个必须不被允许的单元格..我想提一下,我试图从'didSelectRowAtIndexPath'和我保存旧索引路径在'CellForRowAtIndexPath'中检查它,但它不起作用:
if (old == indexpath)
{
cell?.backgroundColor = UIColor.redColor()
cell?.textLabel?.backgroundColor = UIColor.whiteColor() //to change only the thumbnail color and keep the cell color
}
else {
cell?.backgroundColor = UIColor.whiteColor()
}
Run Code Online (Sandbox Code Playgroud)
现在这是我的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = myTable.dequeueReusableCellWithIdentifier("WorkCell") as UITableViewCell
cell.tag = (indexPath.row) + 1
cell.textLabel?.text = Berufs[indexPath.row]
var img = UIImageView(frame: CGRect(x: 10, y: 3, width: 40 , height: 40))
cell.selectionStyle = UITableViewCellSelectionStyle.None
img.image = UIImage(named: "transparent.png")
cell.indentationLevel = 1
cell.indentationWidth = …Run Code Online (Sandbox Code Playgroud)