更新tableView swift中的标头

时间:2015-04-07 04:47:59

标签: ios iphone uitableview swift

我正在尝试更新我在部分标题中创建的label.text,当我添加新行而不重新加载整个部分时。

目前我找到了一个解决方案,但它需要大量 if 语句。 我发现一个部分的标题实际上也是一行,并且行的数量取决于iPhone的cpu架构,如果它是32,则行号是2147483647,如果它是64位,则行号是9223372036854775807。

对于iPhone 5我做

self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow:2147483647, inSection:1)], withRowAnimation: .Fade)

其中2147483647是标题的行号,....很酷,对吧?

现在我知道这不是一个很好的方法,我必须实施手机版本的检查。还有另一种方法可以做到这一点,或者我这样做的方式很好吗?

3 个答案:

答案 0 :(得分:1)

制作UILabel的对象

 var lbl_header = UILabel()


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

    self.lbl_header.frame = CGRectMake(0, 0, 320, 15)
    self.lbl_header.text = "Test"
    self.lbl_header.backgroundColor = UIColor.redColor()
  return self.lbl_header;

  }

现在你要改变UILabel的文字   对于前者我点击按钮更改标签的文字

  @IBAction func update_lbl_click(sender: UIButton)
   {
     self.lbl_header.text = "Hello"
   }

它会将lbl_header的文本更改为Hello ...

答案 1 :(得分:0)

您可以设计自己的标题,就像在表格视图中设计自定义单元格一样。或者你可以像这样添加一个图像:

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

    var myCustomView: UIImageView
    var myImage: UIImage = UIImage(named: "myImageResource")
    myCustomView.image = myImage

    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
    header.addSubview(myCustomView)
    return header
}

然后你可以简单地将你的章节标题UILabel添加为另一个子视图。

答案 2 :(得分:0)

Swift 3更新

#include <stdio.h>
#include <stdint.h>
typedef unsigned char       uint8_t;
typedef short               int16_t;
typedef unsigned short      uint16_t;
typedef int                 int32_t;
typedef unsigned int        uint32_t;
int main(){
    uint8_t ball; 
    uint8_t fool;
    ball=((unsigned char)13);
    fool=((unsigned char)14);
    uint16_t combined_value1=((uint16_t)ball)<<12+((uint16_t)fool)<<8; // WRONG ONE
    uint16_t combined_value2=((uint16_t)ball<<12)+((uint16_t)fool<<8);
    printf("%hu\n",(uint16_t)combined_value1);
    printf("%hu\n",(uint16_t)combined_value2);
    return 0;
}

现在,可以通过从其他地方访问var lbl_header = UILabel() func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { self.lbl_header.frame = CGRect(x: 0, y: 0, width: 200, height: 21) self.lbl_header.text = "Test" self.lbl_header.backgroundColor = UIColor.green return self.lbl_header; } 来更改它。

相关问题