Swift / UITableView将随机行插入部分以发布AdMob广告

时间:2016-09-24 15:52:47

标签: ios swift tableview

我想发布广告。我希望广告位于UITableView内。随机在常规内容中。我的UITableView很容易填充:

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

        return example.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("newEventsCell", forIndexPath: indexPath) as! ExampleCell
        return cell
}

如何将UITableViewCell as! AdCell随机走私到UITableView?非常感谢帮助。

例如:

    [regular content]
-------------------------
    [    ad  cell   ]
-------------------------
    [regular content]
-------------------------
    [regular content]
-------------------------
    [regular content]
-------------------------
    [    ad  cell   ]
-------------------------
    [regular content]
-------------------------

1 个答案:

答案 0 :(得分:3)

您可以创建一个新的原型单元并使用新的标识符(如“Ads”)进行创建,然后您只需修改数组以添加(随机)添加单元格,例如,如果数组是模型,则可以创建变量isAnAds,并在评估要返回的单元格时,如果isAnAds为false,则创建一个显示正常单元格的if条件;如果isAnAds为true,则创建一个“Ads”单元格。我已经使用这种方法在同一个表视图中有2种不同类型的Cell,希望它有所帮助:D