我需要创建一个UITableView
(iOS开发),它会在每个表格单元格旁边显示一个彩色块。
假设我有一个NSMutableArray
对象,其中包含一个名为itemColor
的值,当我填充表格时,如何在包含itemColor的单元格旁边显示一个颜色块?
基本上,我需要一个类似于Twitter显示头像的自定义UITableView
吗?
答案 0 :(得分:2)
我的项目有类似的要求。 这肯定会对你有帮助。
-(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);
}