【发布时间】:2011-06-26 12:39:54
【问题描述】:
你好,这是我的问题:
我从 php 脚本接收到一些数据到我的 tableview。但是当我向下滚动并返回顶部时,我的数据不可读。如果我向下滚动,也一样。数据叠加
这是我的代码:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [messageArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize textSize = { 260.0, 20000.0 };
NSMutableDictionary *dict = [messageArray objectAtIndex:indexPath.row];
NSString *aMsg = [dict objectForKey:@"message"];
CGSize size = [aMsg sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 5;
CGFloat height = size.height<36?36:size.height;
return height;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSMutableDictionary *tempMsg = [messageArray objectAtIndex:indexPath.row];
CGFloat result = 20;
CGSize textSize = { 260.0, 20000.0 };
NSMutableDictionary *dict = [messageArray objectAtIndex:indexPath.row];
NSString *aMsg = [[dict objectForKey:@"message"]stringByReplacingOccurrencesOfString:@"\n" withString:@""];
CGSize size = [aMsg sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
result = MAX(size.height -5.0, 25.0);
// Configure the cell.
CGRect frame = CGRectMake(15, 5, size.width , size.height);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = [tempMsg objectForKey:@"message"];
label.font = [UIFont systemFontOfSize:13];
label.numberOfLines = 0;
[label sizeToFit];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[label release];
[cell setFrame:CGRectMake(20.0, 0.0, size.width, result)];
UIImage* balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width+35, result+10)];
UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
[newImage setImage:balloon];
[newView addSubview:newImage];
[cell setBackgroundView:newView];
return cell;
}
【问题讨论】:
标签: iphone xcode uitableview scroll