【问题标题】:how to dynamically change the textview height and cell height according to the text length without the scroll如何在不滚动的情况下根据文本长度动态更改文本视图高度和单元格高度
【发布时间】:2015-07-28 14:08:27
【问题描述】:

这是我在表格中显示数据的代码,我的完整数据未显示在单元格中。 第一张图片是当我将可滚动设置为无时,第二张图片当我没有设置可滚动时。我是初学者。请帮我解决这个问题。

- (void)textViewDidChange:(UITextView *)textView{
[table beginUpdates];
[table  endUpdates];
}

-(void)createdatabase{

BOOL success;

NSFileManager *filemanager = [NSFileManager defaultManager];

success = [filemanager fileExistsAtPath:datapath];

if (success)return;

NSString *databasefromapp = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:dataname];

[filemanager copyItemAtPath:databasefromapp toPath:datapath error:nil];

}

-(void)getdatabase{

eventitleary = [[NSMutableArray alloc]init];
eventdescary = [[NSMutableArray alloc]init];
eventimgary = [[NSMutableArray alloc] init];

sqlite3 *dataname1;

if (sqlite3_open([datapath UTF8String],&dataname1) == SQLITE_OK) {

    const char *sqlStatement;

    sqlStatement = "SELECT * FROM photography_events";


    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(dataname1, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
    {
        // Loop through the results and add them to the feeds array

        while(sqlite3_step(compiledStatement) == SQLITE_ROW)
        {
            // Read the data from the result row

            NSString *str_title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

            [eventitleary addObject:str_title];

            NSString *str_desc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

            [eventdescary addObject:str_desc];

            NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 2) length:sqlite3_column_bytes(compiledStatement, 2)];
            [eventimgary addObject:data];

        }

    }
}

NSLog(@"%@", eventitleary);
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }

 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;

 }
 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return eventitleary.count;
 }

 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

NSString *cellidenti = @"CellIdenti";

TableViewCell2 *cell = (TableViewCell2*)[tableView dequeueReusableCellWithIdentifier:cellidenti];

if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"TableViewCell2" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
cell.eventitlelbl.text = [eventitleary objectAtIndex:indexPath.row];

cell.eventdesc.text = [eventdescary objectAtIndex:indexPath.row];
cell.eventdesc.editable = NO;
//cell.eventdesc.scrollEnabled = NO;
[cell.eventdesc sizeToFit];

frame = cell.eventdesc.frame;

frame.size = cell.eventdesc.contentSize;

cell.eventdesc.frame = frame;

NSData *dataimg = (NSData*)[eventimgary objectAtIndex:indexPath.row];

cell.eventimg.image = [UIImage imageWithData:dataimg];

return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

// Return the height with a bit of additional padding space
return frame.size.height + 300;
}

【问题讨论】:

标签: ios objective-c uitableview dynamic uitextview


【解决方案1】:

您可以通过自适应布局来实现这一点。 Check this awesome tutorial if you are working with autolayout。否则,您可以通过计算文本适合的高度来设置动态 tableView 单元格高度。

您可以使用以下方法计算文本的高度。传递文本、文本视图所需的字体和宽度。

-(CGFloat)heightForText:(NSString*)text withFont:(UIFont *)font andWidth:(CGFloat)width
{
   CGSize constrainedSize = CGSizeMake(width, MAXFLOAT);
   NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
   NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDictionary];
   CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
   if (requiredHeight.size.width > width) {
    requiredHeight = CGRectMake(0,0,width, requiredHeight.size.height);
   }
   return requiredHeight.size.height;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [self heightForText:@"your text view text for this row" withFont:[UIFont fontWithName:@"Helvetica" size:16] andWidth:320];
}

【讨论】:

  • downvoter 你能给出你投反对票的理由吗?
  • 将文本视图添加到具有动态高度的单元格的绝佳解决方案。
【解决方案2】:

我刚刚得到它,我想动态而不是使用自动布局,这是我的代码。希望将来它适用于任何人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多