swift - 如何在自定义视图单元格中显示两个不同的图像

时间:2017-05-31 22:12:22

标签: ios swift xcode

我在我的视图单元格中有三个图像视图,它们目前都显示相同的图像我想知道如何使每个图像成为单独的图像,也许类似 如果image = imagebanana显示香蕉的图像,如果image = imageapple显示苹果的图像,那么每个单元格中的所有图像都是香蕉,香蕉,香蕉,我想在一个单元格中有香蕉,苹果,梨? / p>

struct cellData {

let cell: Int!
let text: String!
let image: UIImage!   
}



class HomepageVC2JAREDTutorial: UITableViewController {


var arrayofCellData = [cellData]()



override func viewDidLoad() {
    super.viewDidLoad()

    arrayofCellData = [cellData(cell : 1, text : "tom",image : #imageLiteral(resourceName: "tom3")),cellData(cell : 2, text : "denis",image : #imageLiteral(resourceName: "denis2")),cellData(cell : 1, text : "mark",image : #imageLiteral(resourceName: "mark1")),]



}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return arrayofCellData.count
}



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



    if arrayofCellData[indexPath.row].cell == 1{


        let cell = Bundle.main.loadNibNamed("XibCustomCellJared", owner: self, options: nil)?.first as! XibCustomCellJared

        cell.ProfilePictureImageView.image = arrayofCellData[indexPath.row].image
        cell.MoreOptionsImageView.image = arrayofCellData[indexPath.row].image
        cell.AccountTypeImageView.image = arrayofCellData[indexPath.row].image
        cell.DisplayNameLabel.text = arrayofCellData[indexPath.row].text
        cell.StatusSubtitleLabel.text = arrayofCellData[indexPath.row].text

        return cell


    }else if arrayofCellData[indexPath.row].cell == 2{


        let cell = Bundle.main.loadNibNamed("Xib2CustomCellJared", owner: self, options: nil)?.first as! Xib2CustomCellJared

        cell.ProfilePictureImageView.image = arrayofCellData[indexPath.row].image
        cell.MoreOptionsImageView.image = arrayofCellData[indexPath.row].image
        cell.AccountTypeImageView.image = arrayofCellData[indexPath.row].image
        cell.DisplayNameLabel.text = arrayofCellData[indexPath.row].text
        cell.StatusSubtitleLabel.text = arrayofCellData[indexPath.row].text

        return cell

    }else{




            let cell = Bundle.main.loadNibNamed("XibCustomCellJared", owner: self, options: nil)?.first as! XibCustomCellJared

            cell.ProfilePictureImageView.image = arrayofCellData[indexPath.row].image
            cell.MoreOptionsImageView.image = arrayofCellData[indexPath.row].image
            cell.AccountTypeImageView.image = arrayofCellData[indexPath.row].image
            cell.DisplayNameLabel.text = arrayofCellData[indexPath.row].text
            cell.StatusSubtitleLabel.text = arrayofCellData[indexPath.row].text

            return cell



        }

}

2 个答案:

答案 0 :(得分:1)

编辑:我错过了你说你制作了一个自定义视图单元格的标题。无论哪种方式,如果您使用故事板,下面的方法可能适合您。

您是否专门为您的tableview创建了表视图单元格?我认为你可以通过这种方式更轻松地完成你的目标。

表视图单元格(在tableview中标识为原型单元格)中,您可以创建三个单独的图像视图,并为每个视图提供唯一的标识符(标记),如下图所示。创建表格视图单元格时,还必须为视图单元格指定唯一标识符。

表格查看单元格标识符 enter image description here

特定图片视图的标记标识符(可以是任何int) enter image description here

一旦您确定了表格视图单元格,并且您为图像视图标记了数字,我们就可以获得一些代码。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3s/jquery.min.js"></script> <script> $(document).ready(function() $('#sidebar-btn').click(function(){ $('#sidebar').addClass('visible'); }); </script>函数内部,您将创建类似于下图所示的代码:

    body{
    	margin:0px
    	padding:0px;
        font-family: "Helvetic Neue";
        
    }
    #sidebar{
    	background:#151718;
    	width:200px;
    	height:100%;
    	display:block;
    	position:absolute;
    	left:-200px;
    	top:0px;
        transition:left 0.3s linear;

    }
    #sidebar.visible{
        left:0px;
        transition:left 0.3s linear; 
    }
    ul{ 
        margin:0px;
        padding:0px;
    }

    ul li{
        list-style:none;
    }
    ul li a{
        background:#1C1E1F;
        color:#ccc;
        border-bottom: 1px solid #111;
        display: block;
        width:180px;
        padding: 10px;
        text-decoration: none;
    }
    #sidebar-btn{
        display:inline-block;
        vertical-align:middle;
        width:20px;
        height:15px;
        cursor:pointer;
        margin:20px;
        position:absolute;
        top:0px;
        right:-60px;
    }
    #sidebar-btn span{
        height:1px;
        background:#111;
        margin-bottom:5px;
        display: block;
    }
    #sidebar-nth-child(2){
        width: 75%
    }

试试这个,看看它是否适合您。

答案 1 :(得分:0)

您为模型中的单元格创建数据  样本:

 cellData{
   var image1:UIImage; // apple
   var image2:UIImage; //pear
   var image3:UIImage; // banana
   var text:String;
   ....
}

并在cellforrowatindexpath获取数据:

let data = arrayofCellData[indexPath.row] as! cellData  
cell.ProfilePictureImageView.image = data.image1  
cell.ProfilePictureImageView.image = data.image1  
cell.ProfilePictureImageView.image = data.image1