[英]IOS TableView height of rows
我需要2種行。 一個高度為44像素,另一個高度為90像素:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
return 90.0;
else
return 44.0;
}
問題是當我調用-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath時,因為我需要將高度為90px的scrollView添加到90px的單元格中,但是將其添加到中間單元而不是頂部。
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:ScrolledCellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ScrolledCellIdentifier];
//cell.textLabel.text = @"hi";
[cell addSubview:self.scrollView];
}
試圖設置單元格的textLabel起作用,問題在於添加了scrollView。
如果我將此行添加到代碼中:
self.scrollView.frame = CGRectMake(0,-45, 320, 90);
它工作正常,但對我來說聽起來有些奇怪。 怎么了?
在添加scrollView的框架之前,請將其設置為單元格的邊界。
所以
self.scrollView.frame = cell.bounds;
[cell addSubview:self.scrollView];
調試... NSLog ....輸出事物的框架,這樣您就可以看到它們的放置位置,而不是想要放置的位置。 使用它來輸出框架坐標:
NSLog(@"Frame is: ", NSStringFromCGRect(self.scrollView.frame));
或定義一個不錯的快捷方式,因為我一直都在使用
#define LogCGRect(rect) NSLog(@"CGRect:%@", NSStringFromCGRect(rect));
那你就可以做
LogCGRect(self.scrollview.frame);
無論高度是44還是90,它都可以使滾動視圖適合tableviewcell。
我使用以下代碼解決了我的問題:
cell = [tableView dequeueReusableCellWithIdentifier:ScrolledCellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ScrolledCellIdentifier];
cell.frame = CGRectMake(0, 0, 320, 90);
self.scrollView.frame = CGRectMake(0,0, 320, 90);
[cell addSubview:self.scrollView];
我不知道這是不對的,但是可以...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.