में UILabel चौड़ाई की गतिशील गणना मैं UITableViewCellStyle1 (या तो) के आधार पर एक कस्टम, इन-एप्लिकेशन सेटिंग्स दृश्य बना रहा हूं।UITableViewCell
मैं एक सेल के बाएं लेबल (शीर्षक) की चौड़ाई की गतिशील गणना करने की कोशिश कर रहा हूं यह निर्धारित करने के लिए कि दाईं ओर मेरा टेक्स्ट फ़ील्ड कितना चौड़ा हो सकता है।
जब मैं सिम्युलेटर और दृश्य लोड में ऐप लॉन्च करता हूं, तब तक शीर्षक लेबल की चौड़ाई शून्य होती है जब तक कि मैं दृश्य को स्क्रॉल नहीं करता और एक सेल दिखाई नहीं देता है, तब जब मैं बैक अप को स्क्रॉल करता हूं तो उम्मीद के अनुसार समायोजित होता है। मेरा मानना है कि इसे सेल पुन: उपयोग के साथ कुछ करना पड़ सकता है।
मुझे दृश्य लोड होने पर कोशिकाओं में शीर्षक लेबल की आवश्यकता होती है, जब वे एक बार दृष्टि से बाहर नहीं निकलते हैं।
- (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;
// constrainedSize.width = MAXFLOAT;
// constrainedSize.height = MAXFLOAT;
CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font /*constrainedToSize:constrainedSize*/];
NSLog(@"text label width: %f", textLabelSize.width);
// Set the accessory view
BAESettingsTableCellAccessoryType accessoryType = [[[tableSection objectForKey:@"itemAccessories"] objectAtIndex:indexPath.row] integerValue];
switch (accessoryType)
{
case BAESettingsTableCellAccessoryDisclosureIndicator:
{
UIImageView* disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AccessoryDisclosure.png"]];
[cell setAccessoryView:disclosureView];
[disclosureView release];
break;
}
case BAESettingsTableCellAccessoryOnOffSwitch:
{
UISwitch* onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[onOffSwitch setOn:YES];
[cell setAccessoryView:onOffSwitch];
[onOffSwitch release];
break;
}
case BAESettingsTableCellAccessoryNone:
// default:
// break;
{
CGRect cellFrame = [cell frame];
float detailTextFieldWidth = cellFrame.size.width - (textLabelSize.width + 50);
float detailTextFieldHeight = cellFrame.size.height - 1;
NSLog(@"detail text field calculated frame width, height, %f, %f", detailTextFieldWidth, detailTextFieldHeight);
CGRect frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, detailTextFieldWidth, detailTextFieldHeight);
UITextField* textField = [[UITextField alloc] initWithFrame:frame];
NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row];
textField.text = detailLabelString;
textField.textColor = cell.detailTextLabel.textColor;
textField.textAlignment = UITextAlignmentRight;
textField.borderStyle = UITextBorderStyleRoundedRect;
[cell setAccessoryView:textField];
[textField release];
break;
}
}
return cell;
}
यह काम नहीं करता – Dejell
अद्भुत विचार। [सेल लेआउट IfNeeded]; बहुत अच्छा काम करता है! अन्य समाधान काम नहीं करते हैं। आपका बहुत बहुत धन्यवाद! – Vyacheslav