繁体   English   中英

如何在Swift iOS的侧面菜单中添加个人资料图片

[英]How to add profile image to side menu in swift ios

我有使用tableview的侧边菜单,它运作良好,但我不知道如何快速将圆形轮廓图像添加到侧边菜单。 请帮我怎么做。这是我的代码,

  override function tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
    var cell1 = tableView.dequeueReusableCellWithIdentifier("Cell1") as? UITableViewCell

    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CELL")
        cell!.backgroundColor = UIColor.clearColor()
        cell!.textLabel?.textColor = UIColor.whiteColor()
        let selectedBackgroundView = UIView(frame: CGRectMake(0, 0, cell!.frame.size.width, cell!.frame.size.height))
        selectedBackgroundView.backgroundColor = UIColor.grayColor().colorWithAlphaComponent(0.2)
        cell!.selectedBackgroundView = selectedBackgroundView
    }
    cell!.textLabel?.font = UIFont.systemFontOfSize(14)
    cell!.textLabel?.text = self.items[indexPath.row];

    var imageView = UIImageView(frame: CGRectMake(5, 10, 20, 20));
    var image = UIImage(named:images[indexPath.row]);
    imageView.image = image;
    cell!.imageView?.image = imageView.image

    return cell!
}

谢谢

尝试这个

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        return 70 // Please change its according to you 
 }

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
    var view = UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 70))
    view.backgroundColor = UIColor.clearColor()

    var profileImageView = UIImageView(frame: CGRectMake(40, 5, 60, 60)) // Change frame size according to you ..
    profileImageView.image = UIImage(named: "image name") //Image set your
    view.addSubview(profileImageView)

    return view
}

它将正常工作..根据您的情况更改较小

您可以使用

目标c

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section
{
   UIView *headerView = // the size of the header you want
   UIImageView *userImageView = // the size of the UserImage you want
   userImageView.layer.cornerRadius = userImageView.bounds.size.width/2;
   userImageView.image = // the userImage
   [headerView addSubView:userImageView];
   return headerView;
}

迅速

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
    var headerView = UIView( // size you want
    var userImageView = UIImageView(frame: CGRectMake(// size you want)) 
    userImageView.image = // user image
    userImageView.layer.cornerRadius = userImageView.bounds.size.width/2'
    headerView .addSubview(userImageView )
    return headerView 
}

不要忘记相应设置标题的高度

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM