程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-iphone-關於避免內容覆蓋

ios-iphone-關於避免內容覆蓋

編輯:編程綜合問答
iphone-關於避免內容覆蓋

怎麼在UItableView的單元中避免內容覆蓋?

//在表中顯示內容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
     [tableView deselectRowAtIndexPath:indexPath animated:NO];
    static NSString *CellIdentifier = @"Cell";      
    UITableViewCell *cell = [tableView       dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }

    UIImage  *ibnLogo = [[UIImage imageNamed:@"IBN.jpeg"]autorelease];

    News *news= [[xmlParser newsArray] objectAtIndex:indexPath.row];

    CGRect imageFrame = CGRectMake(2, 8, 40, 40);
    self.customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
    self.customImage.image = ibnLogo;
    [cell.contentView addSubview:self.customImage];

    CGRect contentFrame = CGRectMake(45, 2, 265, 30);
    UILabel *contentLabel = [[[UILabel alloc] initWithFrame:contentFrame] autorelease];
    contentLabel.numberOfLines = 2;
    contentLabel.font = [UIFont italicSystemFontOfSize:12];
    contentLabel.text = [news content];
    [cell.contentView addSubview:contentLabel];

    CGRect dateFrame = CGRectMake(45, 40, 265, 10);
    UILabel *dateLabel = [[[UILabel alloc] initWithFrame:dateFrame] autorelease];
    dateLabel.font = [UIFont systemFontOfSize:10];
    dateLabel.text = [news dateCreated];
    [cell.contentView addSubview:dateLabel];    
    return cell;
}

最佳回答:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
         [tableView deselectRowAtIndexPath:indexPath animated:NO];
        static NSString *CellIdentifier = @"Cell";      
        UITableViewCell *cell = [tableView       dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;

             UIImage  *ibnLogo = [[UIImage imageNamed:@"IBN.jpeg"]autorelease];

        News *news= [[xmlParser newsArray] objectAtIndex:indexPath.row];

        CGRect imageFrame = CGRectMake(2, 8, 40, 40);
        self.customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
        self.customImage.image = ibnLogo;
        [cell.contentView addSubview:self.customImage];

        CGRect contentFrame = CGRectMake(45, 2, 265, 30);
        UILabel *contentLabel = [[[UILabel alloc] initWithFrame:contentFrame] autorelease];
        contentLabel.numberOfLines = 2;
        contentLabel.font = [UIFont italicSystemFontOfSize:12];
        contentLabel.text = [news content];
        [cell.contentView addSubview:contentLabel];

        CGRect dateFrame = CGRectMake(45, 40, 265, 10);
        UILabel *dateLabel = [[[UILabel alloc] initWithFrame:dateFrame] autorelease];
        dateLabel.font = [UIFont systemFontOfSize:10];
        dateLabel.text = [news dateCreated];
        [cell.contentView addSubview:dateLabel];

        }

        return cell;
    }

將所有的元素添加到if (cell == nil)條件中

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved