2016-06-30 4 views
0

У меня есть UITableView с двумя разделами (каждая секция имеет n количество строк). У меня есть требование добавить цвет градиента только в первый раздел. Есть ли способ добавить цвет градиента в paticular разделе UITableView?Цвет градиента на конкретном участке

+0

вы пробовали это? http://stackoverflow.com/questions/8413436/change-uitable-section-backgroundcolor-without-loosing-section-title –

+0

Мое требование не изменять вид заголовка/нижнего колонтитула раздела. У меня есть ячейки таблицы с четким цветом, и я хочу показать цвет градиента в определенном разделе вида таблицы –

ответ

0

Вы можете просто сравнить, в каком разделе вы хотите показать цвет градиента. Пример:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let view: UIView = UIView(frame: CGRectMake(0.0, 0.0, 320.0, 50.0)) 

    if section == 0 { // for your gradient section. it can be any section that you want 

     let gradient: CAGradientLayer = CAGradientLayer() 
     gradient.frame = view.bounds 
     gradient.colors = [UIColor.whiteColor().CGColor, UIColor.blackColor().CGColor] 
     view.layer.insertSublayer(gradient, atIndex: 0) 

    } else { 
     view.backgroundColor = UIColor.lightGrayColor() 

    } 
    return view 
} 
0

Apple только позволяет изменить верхний или нижний колонтитул, а не вид, который вы хотите. Вместо этого вы можете установить градиент к каждой ячейке в разделе 0

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let Identifier = "RWCellIdentifier" 
    var cell = tableView.dequeueReusableCellWithIdentifier(Identifier) 
    if (cell == nil) { 
     cell = UITableViewCell(style: .Default, reuseIdentifier: Identifier) 
    } 
    cell?.textLabel?.text = "text test" 

//gradient color 
    if indexPath.section == 0 { 
     let gradient: CAGradientLayer = CAGradientLayer() 
     gradient.frame = CGRectMake(0, 0, self.view.frame.width, cell!.frame.height) 
     gradient.colors = [UIColor.redColor().CGColor, UIColor.yellowColor().CGColor] 
     cell!.layer.insertSublayer(gradient, atIndex: 0) 
    } 
    return cell! 
} 

here's how it looks