UITableView цветовой блок - PullRequest
       8

UITableView цветовой блок

0 голосов
/ 08 декабря 2011

Мне нужно создать UITableView (iOS Development), который будет отображать цветной блок рядом с каждым элементом ячейки таблицы.

Скажем, у меня есть объект NSMutableArray, который содержит значение с именем itemColor, как, когда я заполняю свою таблицу, отображать ли блок цвета рядом с ячейкой, содержащей itemColor?

В общем, мне нужен пользовательский UITableView, аналогичный тому, как Twitter отображает аватары?

1 Ответ

2 голосов
/ 08 декабря 2011

У меня было похожее требование в моем проекте. Это вам определенно поможет.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier;
    UITableViewCell *cell;
    cellIdentifier = [NSString stringWithFormat:@"Cell Identifier"];

    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        cell = [self tableViewCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    }
    [self configureCell:cell forIndexPath:indexPath];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
    CGRect rect;
    UILabel *label;
    UIView *backView;
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

    float center = (float) (CELL_HEIGHT-MAINFONT_SIZE)/2;

    rect = CGRectMake(15, center, 150, MAINFONT_SIZE);
    label = [[UILabel alloc] initWithFrame:rect]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    label.tag = NAME_ID_TAG;
    label.font = [UIFont boldSystemFontOfSize:MAINFONT_SIZE];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    //label.highlightedTextColor = [UIColor whiteColor];
    [cell.contentView addSubview:label];
    [label release];

    rect = CGRectMake(160, 10, 150, CELL_HEIGHT-20);
    backView = [[UIView alloc] initWithFrame:rect];
    backView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |     UIViewAutoresizingFlexibleWidth;
    backView.backgroundColor = [UIColor clearColor];
    backView.tag = BACK_VIEW_TAG;



    [cell.contentView addSubview:backView];
    [backView release];

    return cell;
}



-(void)configureCell:(UITableViewCell *)tableViewCell forIndexPath:(NSIndexPath *)indexPath{
    //DeviceInfo *device;
    UILabel *label;
    PatientInfo *patInfo;
    UIView *backView;
    patInfo = [patientarray objectAtIndex:indexPath.row];

    label = (UILabel *)[tableViewCell viewWithTag:NAME_ID_TAG];
    label.text = patInfo.patientName;

    backView = (UIView *)[tableViewCell viewWithTag:BACK_VIEW_TAG];


       backView.backgroundColor = UIColorFromRGB(patInfo.itemColor);


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...