簡體   English   中英

自定義單元格UITableView有問題

[英]Having trouble with customized cell UITableView

我正在嘗試添加一個customCell,它只允許我左對齊一個字符串,然后右對齊另一個字符串。 在上一個問題中,有人提出了以下建議,但我在使用我的代碼時遇到了問題。 Xcode說:RootviewController可能無法響應--contentView,調用[[self contentView] addSubView:item]或[[self contentView] addSubView:rank]會在運行時崩潰我的應用程序

// Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

            UILabel *rank = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20];
            //Mess around with the rects, I am just merely guessing.
            [rank setTag:5];
            [[self contentView] addSubView:rank];
            [rank release];


            UILabel *item = [[UILabel alloc] initWithFrame:CGRectMake(110, 5, 220, 20];
            //Important
            [item setTextAlignment:UITextAlignmentRight];
            [item setTag:6];
            [[self contentView] addSubView:item];
            [item release];
        }

        UILabel *rank = (UILabel *)[cell viewWithTag:5];
        UILabel *item = (UILabel *)[cell viewWithTag:6];

        rank = @"leftside";
        item = @"rightside";

    }

謝謝你的任何想法

self在這里是控制器。 您應該將contentView發送到剛剛創建的單元格,而不是將self設為接收器。

你應該看看

[cell.contentView addSubview:item];
[cell.contentView addSubview:rank];

rank.text = @"leftside";
item.text = @"rightside";

還有一點需要注意。 如果您的UITableView是scrollEnabled ,那么您將遇到cellReusability問題,並且您的標簽將與隨后的滾動混亂。 我建議你CustomUITableViewCells UITableViewCells並在布局中添加它們,然后使用CustomUITableViewCells

暫無
暫無

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

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