天天看點

UITableView不能顯示detailtext

起因很簡單,就是跟着教程做,熟悉UITableView的使用,看到書上說cell有textLabel , imageview ,還有一個detailTextLabel,前面兩個知道,但是後一個有點好奇,于是就試了一下,結果編譯倒是沒有問題,但是沒有顯示detailTextLabel,于是google吧,終于在這裡 找到了答案。其實就是在初始化cell使用的參數不合适,換成下面代碼中的

initWithStyle:UITableViewCellStyleSubtitle
           

就可以了。不行你試試。原諒我的low,為了認識什麼叫header和footer我把它們也給列印出來了。

UITableView不能顯示detailtext
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];


    static NSString *simpleTableIdentifier = @"AnimalsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if(cell == nil)
    {
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];

    }
    cell.textLabel.text = [animals objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = @"this is a detail text";

    /*
    //if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        if ([cell.detailTextLabel.text length]==0)
        {
            cell.detailTextLabel.text = @"detail ";
        }
        NSLog(@"detail.lenght=%lu",[cell.detailTextLabel.text length]);
        [cell layoutSubviews];
    }
    */
    
    // Configure the cell...
    
    return cell;
}
           

補充說一下,其他類型的 style 在下面的官方連結中有具體的介紹 點選打開連結