我正在构建一个基于UITableViewCellStyle1(或左右)的自定义应用程序内设置视图.
我试图动态计算单元格左侧标签(标题)的宽度,以确定右侧文本字段的宽度.
当我在模拟器中启动应用程序并加载视图时,标题标签的宽度为零,直到我向下滚动视图并且单元格不可见,然后当我向后滚动时,大小按预期调整.我相信这可能与细胞重复使用有关.
我需要单元格中的标题标签在视图加载时具有正确的宽度,而不是在它们一次看不见之后.
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* reuseIdentifier = @"SettingsCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if ( !cell )
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier] autorelease];
[cell setBackgroundColor:[UIColor baeDarkGrayColor]];
UILabel* cellLabel = [cell textLabel];
[cellLabel setTextColor:[UIColor whiteColor]];
[cellLabel setBackgroundColor:[UIColor clearColor]];
[cell setUserInteractionEnabled:YES];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
// Populate cell with corresponding data.
NSDictionary* tableSection = [_tableData objectAtIndex:indexPath.section];
// Set the label
UILabel* textLabel = [cell textLabel];
NSString* labelString = [[tableSection objectForKey:@"itemTitles"] objectAtIndex:indexPath.row];
[textLabel setText:labelString];
// CGSize constrainedSize; …Run Code Online (Sandbox Code Playgroud) 我在NSDictionary对象中有我的数据,其中键是CGPoints转换为NSValues,对象是UIColors.这是我用来从字典中返回一个对象的方法:
- (UIColor*) getTemperatureColor2 {
NSDictionary* temperatureColorMap = [Weather getTemperatureColorMap];
for(id key in temperatureColorMap) {
CGPoint point = [key CGPointValue];
if ( (int)roundf(self.temperature_celsius) >= (int)roundf(point.x) ) {
if ( (int) roundf(self.temperature_celsius) <= (int) roundf(point.y) ) {
return [temperatureColorMap objectForKey:key];
}
}
}
return [UIColor blackColor];
}
Run Code Online (Sandbox Code Playgroud)
这是getTemperatureColorMap方法,在同一个类(Weather)中实现:
+ (NSDictionary*) getTemperatureColorMap {
static NSDictionary* temperatureColorMap = nil;
if (temperatureColorMap == nil) {
temperatureColorMap = [[[NSDictionary alloc] initWithObjectsAndKeys:
RGB2UIColor(0x0E09EE), [NSValue valueWithCGPoint: CGPointMake(-99, -8)],
RGB2UIColor(0xB85FC), [NSValue valueWithCGPoint: CGPointMake(-7, -3) ],
RGB2UIColor(0x0BDCFC), [NSValue valueWithCGPoint: …Run Code Online (Sandbox Code Playgroud)