كيف يمكنني إنشاء متغير الحجم UITableViewCell?
-
22-07-2019 - |
سؤال
أنا لا يمكن أن يبدو للحصول على النص في الواقع تمتد عدة خطوط.مرتفعات تبدو صحيحة.ما أنا في عداد المفقودين ؟
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"StatusCell"] autorelease];
CGRect frame = cell.contentView.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
myLabel.text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
[cell.contentView addSubview:myLabel];
[myLabel release];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize withinSize = CGSizeMake(tableView.frame.size.width, 1000);
CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
أيضا, ما أنا في عداد المفقودين أن يجعل التسميات تظهر أطول من الجدول الخلية ؟
المحلول
Tweetero
مثالا على هذا في MessageListController.m
.رمز هناك يجعل الشاشة التالية:
(صورة مأخوذة من ماشابل).
الأساسية تنفيذ مخطط:
عند بناء
UITableViewCell
, إنشاء و إضافةUILabel
كما subview على النحو المبين فيtableviewCellWithReuseIdentifier:
.ابحث عن إنشاءTEXT_TAG
التسمية.عندما إثراء
UITableViewCell
مع إطلالات على ضمان تنسيق تسمية بشكل صحيح ، كما هو الحال فيconfigureCell:forIndexPath
, وبالمثل ابحث عن تسمية الوسمTEXT_TAG
.عودة الارتفاع المناسب لكل خلية ، كما هو الحال في
tableView:heightForRowAtIndexPath
.
نصائح أخرى
myLabel.numberOfLines = 2;
وتحقق في مستندات للحصول على معلومات كاملة حول كيفية استخدام هذه الخاصية.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return theSizeYouWantYourCellToBe;
}