【发布时间】:2011-11-30 23:01:22
【问题描述】:
我正在使用此代码输出一个带有 UILabel 的 tableView,它可以复制列的外观。
目前有 2 个标签,但我要添加第三个标签,因此我需要将它们缩小一点,以便所有 3 个标签都适合。
我注释掉了添加第三个“列”的 2 行,该“列”将显示用户名文本。
如何调整代码,以便在 UITableView 中容纳 3 列而不是 2 列。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *MyIdentifier = [NSString stringWithFormat:@"AuditGridCell %i", indexPath.row];
GridTableViewCell *cell = (GridTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[GridTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
[(GridTableViewCell *)cell clearColumns];
}
int is_row_bold = 0;
NSMutableArray *values = [[NSMutableArray alloc] init];
float sizes[10] = {CELL_CONTENT_WIDTH, 250.0, 100.0, 125.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
if ([self interfaceOrientation] == UIInterfaceOrientationPortrait ||
[self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown) {
sizes[0] = CELL_CONTENT_WIDTH_PORTRAIT;
sizes[1] = 200.0;
}
float sum_total = 0.0;
if (1) {
int spacer_width = 5;
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, spacer_width, tableView.rowHeight)] autorelease];
[cell.contentView addSubview:label];
sum_total += spacer_width;
}
if (0 == indexPath.row) {
is_row_bold = 1;
// [values addObject:@"Username"];
[values addObject:@"Description"];
[values addObject:@"Date"];
} else {
int index = indexPath.row - 1;
// [values addObject:[[self.auditRecords objectAtIndex:index] valueForKey:@"username"]];
[values addObject:[[self.auditRecords objectAtIndex:index] valueForKey:@"description"]];
[values addObject:[[self.auditRecords objectAtIndex:index] valueForKey:@"date"]];
}
for (int it=0; it < [values count]; ++it) {
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(sum_total, 0.0, sizes[it], tableView.rowHeight)] autorelease];
sum_total += sizes[it];
[(GridTableViewCell*)cell addColumn:sizes[it]];
label.text = [values objectAtIndex:it];
[cell.contentView addSubview:label];
}
[values release];
return cell;
}
【问题讨论】:
-
究竟是什么问题?
-
如何调整代码,以便可以容纳 3 列而不是 2 列。
标签: iphone objective-c uitableview uilabel