如何在 swift ios 中将个人资料图片添加到侧边菜单
How to add profile image to side menu in swift ios
我有使用 tableview 的侧边菜单,它运行良好,但我不知道如何在 swift 中将圆形个人资料图像添加到侧边菜单。请帮我怎么做this.here是我的代码,
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
}
它会很好..根据你改变小调
您可以使用
Objective 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;
}
Swift
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
}
不要忘记相应地设置页眉的高度
我有使用 tableview 的侧边菜单,它运行良好,但我不知道如何在 swift 中将圆形个人资料图像添加到侧边菜单。请帮我怎么做this.here是我的代码,
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
}
它会很好..根据你改变小调
您可以使用
Objective 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;
}
Swift
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
}
不要忘记相应地设置页眉的高度