自定义tableviewcell(二)

景景胜
2023-12-01

//显示view

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

    static NSString *CellTableIdentifier = @"CellTableIdentifier";
    static BOOL nibsregistered = NO;
    if(!nibsregistered)
    {
        UINib *nib = [UINib nibWithNibName:@"BIDNameAndColorCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifier];
        nibsregistered=YES;
    }
    BIDNameAndColorCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.computers objectAtIndex:row];
    cell.name = [rowData objectForKey:@"Name"]; cell.color = [rowData objectForKey:@"Color"];
    return cell;

}



//再storyboard中

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

{

    static NSString * identify=@"MealCell";

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identify];

    if(cell==nil)

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];

    }

    NSUInteger row = [indexPath row];

    UILabel *name=(UILabel *)[cell viewWithTag:1];

    UILabel *distance=(UILabel *)[cell viewWithTag:2];

    UILabel *detail=(UILabel *)[cell viewWithTag:3];

    UIImageView *imageview=(UIImageView *)[cell viewWithTag:4];

    name.text=[list_name objectAtIndex:row];

    distance.text=[list_distance objectAtIndex:row];

    detail.text=[list_detail objectAtIndex:row];

    UIImage *image=[UIImage imageNamed:@"qq.png"];

    imageview.image=image;

    return cell;

}


 类似资料: