相关疑难解决方法(0)

在tableview中选择多行并勾选所选行

我正在从plist文件加载tableView.这没有问题.我只是想"勾选"选定的行.目前,我的代码没有按预期工作.目前,它看起来如下:

  • 点击row1(它会勾选第1行=好)
  • 再次点击row1(没有任何事情发生=糟糕.我希望这里的行不被攻击)在第1行再次点击时,它会取消.第二次点击它之后.
  • 当我在tableview的初始加载时点击row0时,它永远不会勾选我的行

我的代码:

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)

swift

22
推荐指数
5
解决办法
4万
查看次数

滚动时,UITableView选定的单元格不会保持选中状态

我在滚动表时表格视图单元格没有保持"选中"状态时遇到问题.这是相关代码:

@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)

uitableview ios

18
推荐指数
2
解决办法
2万
查看次数

在SWIFT中滚动时,我的表视图重用所选单元格

早上好

我的问题是,当我向下和向上滚动时,我的表视图重用所选单元格.我的意思是当我从上向下选择一个单元格然后向下滚动时,我选择的一些单元格被选中,也有一些选中的单元格是当我再次向上滚动时没有显示选中,当发生这种情况时我再次选择苹果不仅仅是一个必须不被允许的单元格..我想提一下,我试图从'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)

uitableview ios swift

7
推荐指数
1
解决办法
9106
查看次数

标签 统计

ios ×2

swift ×2

uitableview ×2