【iOS開發-64】微博案例練習:用代碼自訂cell,主要內容是針對不同高度的cell,ios-64
(1)效果
(2)原始碼和素材下載
http://download.csdn.net/detail/wsb200514/8089727
(3)總結
——可以利用xib布局cell,但是這種情況的cell時固定高度
——如果每個cell是不同高度,則需要用到代碼,核心思想是根據cell裡面子控制項的高度計算這個cell的高度
——這裡面用到的知識點之一,計算一段文字所佔據的高寬,就是確定文字大小,文字寬度之後,可以利用一個方法計算出這段文字的寬高,當然這個方法返回的東西很多,size只是其中之一的屬性。
CGSize nameMaxSize=CGSizeMake(MAXFLOAT, MAXFLOAT); NSDictionary *nameAttr=@{NSFontAttributeName:[UIFont systemFontOfSize:14]}; CGSize nameSize=[self.status.name boundingRectWithSize:nameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nameAttr context:nil].size;
——知識點之二,因為這個項目一開啟的首頁就是一個滾動的視圖也就是一個tableView,所以我們可以刪除項目原先的ViewController,建立一個tableViewController作為主入口。把這個控制器作為主入口的設定是:is Initial View Controller,打鉤即可。(刪除這個控制器內建的cell)
——知識點之三,還是封裝。模型、視圖、控制器各司其職。
——對於只提供外界訪問的變數,即只有getter的那些變數,在@property時候增加一個readonly,防止被其他人修改。
——plist裡面的number類型資料的1和0,可以被直接轉換成BOOL 的0和1。
——修改cell高度的除了直接用tableView.cellHeight這個屬性之外,還有一個代理方法,就是本例中用到的方法。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{}
——對於部分控制項,如果有的話就顯示,如果沒有就隱藏,這裡面hidden屬性至關重要,而且hidden的YES和NO最好是成對出現。因為這些cell會被放入緩衝池,再次取出來使用的時候如果沒有判斷是否顯示則會出錯。
ios開發中 為何系統的cell 跟我自訂的cell 同時出現了?
沒有分也沒關係,都是學開發的,互相協助~
但是你的這個問題問的太籠統了,這個要看代碼具體怎麼寫的,那麼簡單的描述沒法給出正確的答案。把詳細代碼發上來,才能看出問題所在。
ios開發,這個tableView的group是怎做的,用代碼完整的寫出來,
- (void)viewDidLoad{ [super viewDidLoad]; self.title = @"個人設定";// Do any additional setup after loading the view. [self drawTableView];}-(void)drawTableView{ UITableView *tview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) style:UITableViewStyleGrouped]; [tview setDelegate:self]; [tview setDataSource:self]; [self.view addSubview:tview];}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 44;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0||section == 3) { return 2; } return 1;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 4;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ int section = indexPath.secion; int row = indexPath.row; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; switch (section) { case 0: if(row == 0) { cell.textLabel.text = @"設定檔"; }else{ cell.textLab......餘下全文>>