UITableView-(單元格的自定義方法)

//contentViewide

//行內容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //從重用隊列中取出閒置單元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //判斷cell是否爲nil
    if (cell == nil) {
        //建立單元格的時候,要確保重用標示符跟獲取閒置單元格的時候一致
        cell = [[UITableViewCell alloc] initWithStyle:indexPath.row % 4 reuseIdentifier:identifier];
        //添加右邊的圖標
        UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(tableView.frame) - 60 - 20, 10, 60, 60)];
        [cell.contentView addSubview:iconView];
        iconView.tag = 101;
        
        //添加左邊的標題Label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, CGRectGetWidth(cell.frame) - 100, 60)];
        [cell.contentView addSubview:titleLabel];
        titleLabel.tag = 102;
    }
    
    UIImageView *iconView = [cell.contentView viewWithTag:101];
    UILabel *titleLabel = [cell.contentView viewWithTag:102];
    
    //設置內容
    NSString *iconName = [NSString stringWithFormat:@"icon%ld.jpg",indexPath.row % 6];
    iconView.image = [UIImage imageNamed:iconName];
    
    titleLabel.text = self.datas[indexPath.row];
    
    return cell;
    
}
相關文章
相關標籤/搜索