簡體   English   中英

如何使添加到 UITableViewCell 內容視圖的 UIView 符合其范圍?

[英]how to make a UIView added to UITableViewCell content view fit within its bounds?

如何使添加到 UITableViewCell 內容視圖的 UIView 符合其范圍?

也就是說,我創建了一個 NIB 文件(上面有 3 個標簽),並且想用它來顯示 UITableView 中的每個單元格。 我在 cellForRowAtIndexPath 方法中將基於自定義 NIB 的視圖添加到單元格的內容視圖中,但是我看到的最終結果是一 (1) 個基於自定義 NIB 的視圖(不像我預期的那樣在 tableview 中有多個)。

如何安排每個自定義視圖整齊地適合每個 UITableViewCell? 另請注意,自定義 NIB 視圖中的標簽有自動換行。

我是否必須為自定義 NIB 視圖創建一個框架,但在這種情況下,我不確定如何設置坐標。 它會與 UITableViewCell 相關嗎? 如果自定義視圖高度由於它的 UILabel 自動換行而發生變化,那么我假設我必須單獨在 UITableView 中手動計算這個高度? (也許我應該 go 來創建動態/自定義視圖並放棄使用 InterfaceBuilder/NIB 的概念)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    UIView *detailedView = [[[NSBundle mainBundle] loadNibNamed:@"DetailedAppointView" owner:self options:nil] objectAtIndex:0];
    [cell.contentView addSubview:detailedView];  // DOESN'T SEE TO WORK

    return cell;
}

如果我需要實現自定義 UITableViewCell,我就是這樣做的。

我為我的自定義單元格創建了UITableViewCell的子類。 “我的自定義單元”。 然后我為其創建一個 NIB 文件,並在此 NIB 文件中插入UITableViewCell並將其類型更改為“MyCustomCell”,並為其提供一個與 class 名稱同名的標識符。 然后我將所有子視圖插入到我的單元格中。 全部在IB。

在我把我的手機拉到我喜歡的地方之后:-)我以下列方式使用它:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCustomCell";
    static NSString *CellNib = @"MyCustomCell";

    MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (MyCustomCell *)[nib objectAtIndex:0];
    }

    //Manipulate your custom cell here

    return cell;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM