天天看點

IOS自定義UITableViewCell

在用到UITableVIew的時候,經常會自定義每行的Cell

在IOS控件UITableView詳解中的下面代碼修改部分代碼就可以實作自定義的Cell了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
           

自定義代碼:

static NSString *CellWithIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
    }
    
    
    NSUInteger row = [indexPath row];
    // 自定義Cell中Image
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 24, 24, 24)];
    imageView.image = [UIImage imageNamed:@"green.png"];
    [cell.contentView addSubview:imageView];
    [imageView release];
   
    // 自定義文本資訊
    UILabel *city = [[UILabel alloc] initWithFrame:CGRectMake(50, 25, 100, 20)];
    NSString *cityString = [[NSString alloc] initWithFormat:@"城市:%@",[self.dataList objectAtIndex:row]];
    city.text = cityString;
    [cell.contentView addSubview:city];
    [cityString release];
    
//    cell.textLabel.text = [self.dataList objectAtIndex:row];
//    cell.imageView.image = [UIImage imageNamed:@"green.png"];
//    cell.detailTextLabel.text = @"詳細資訊";
//    cell.accessoryType = UITableViewCellSelectionStyleGray;
           
IOS自定義UITableViewCell

繼續閱讀