【发布时间】:2015-04-07 05:35:58
【问题描述】:
我有一个关于在 tableView Cell 中添加子视图的小问题。
我的单元格背景从百分比计数来看是动态的..
我得到正确的百分比,我可以完美地设置背景,但是当我添加一个新条目时......一切都搞砸了
我得到的新条目是完美的,但即使在我调整大小之后,之前的单元格仍然保持不变。
神奇的事情是当我停下来并按照自己的方式构建它时..
请帮帮我..
我使用下面的代码来实现这个...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Dequeue the cell.
speedoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"speedoCell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
CGRect rect = cell.frame;
NSLog(@"Cell width = %f",rect.size.width);
float cell_width=rect.size.width;
float per;
float viewSize=0.0;
if (self.recent_calls.count == 0) {
cell.title.text =@"No records";
cell.desc.text =@"";
}
else{
NSInteger indexOfFirstname = [self.dbManager.arrColumnNames indexOfObject:@"ex_category"];
NSInteger indexOfSecondname = [self.dbManager.arrColumnNames indexOfObject:@"totle"];
NSString *totle_cat=[NSString stringWithFormat:@"%@", [[self.recent_calls objectAtIndex:indexPath.row] objectAtIndex:indexOfSecondname]];
int x=[totle_cat intValue];
NSLog(@"%d--yoyoyoyoyoyoyoyoyoyo--%d",x,amount_totle);
float result=0;
result=((float)x/(float)amount_totle)*100;
// cell_width is static = 343.0 ..... result is percentage we found
viewSize=(cell_width*result)/100;
NSLog(@"%f----------",viewSize);
UIView *view,*view1;
view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
view1.frame=CGRectMake(5, 5, cell.frame.size.width-10, cell.frame.size.height-3);
[view1.layer setCornerRadius:8.0f];
[view1.layer setMasksToBounds:YES];
[view1.layer setBorderWidth:0.5f ];
CGRect frame = view.frame;
frame.size.width = viewSize;
frame.size.height=cell.frame.size.height-3;
view.frame = frame;
NSLog(@"%f",view.frame.size.width);
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"road_panorama1.jpg"]]];
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.frame = CGRectMake(0, 0, viewSize, cell.frame.size.height-3);
[view addSubview:visualEffectView];
[view1 setBackgroundColor:[UIColor whiteColor]];
[view1 insertSubview:view atIndex:0];
[cell.contentView insertSubview:view1 atIndex:0];
UIImage *image;
if ([category isEqualToString:@"Travel"]) {
image=[UIImage imageNamed:@"taxi13.png"];
}
else if ([category isEqualToString:@"Food"]) {
image=[UIImage imageNamed:@"fastfood6.png"];
}
else if ([category isEqualToString:@"Medical"]) {
image=[UIImage imageNamed:@"stethoscope11.png"];
}
else if ([category isEqualToString:@"Shopping"]) {
image=[UIImage imageNamed:@"shopping-cart13.png"];
}
else{
image=[UIImage imageNamed:@"question-mark2.png"];
}
[cell.img setImage:image];
}
return cell;
}
从第二个视图控制器添加条目
NSString *query= [NSString stringWithFormat:@"insert into ex_man_last (ex_title,ex_amount,ex_description,ex_date,ex_category,ex_upload_date,ex_image,ex_my) values ('%@','%@','%@','%@','%@','%@','%@','%@')",_ex_title.text,_ex_amount.text,_ex_description.text,_ex_date.text,_ex_category.text,datestring,imageName,my ];
// Execute the query.
[self.dbManager executeQuery:query];
并调用该方法从视图中的数据库获取更新的条目将出现方法..
NSString *query =[NSString stringWithFormat:@"SELECT *,sum(ex_amount) as totle FROM ex_man_last where ex_my='%@' GROUP BY ex_category ",query_date];
if (self.recent_calls != nil ) {
self.recent_calls = nil;
}
self.recent_calls = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
NSLog(@"--->%@",_ex_main);
NSLog(@"%lu",(unsigned long)[self.recent_calls count]);
NSInteger indexOfSecondname = [self.dbManager.arrColumnNames indexOfObject:@"totle"];
for (int i=0; i<[self.recent_calls count]; i++) {
NSString *amount=[[self.recent_calls objectAtIndex:i] objectAtIndex:indexOfSecondname] ;
amount_totle=amount_totle+[amount intValue];
}
整个代码在索引路径方法的行的单元格中......
请帮帮我...提前谢谢...
【问题讨论】:
-
对于您的重新启动,它工作正常,因为单元格中的代码是正确的,您如何向表格视图添加新条目是您确保重新加载表格视图。跨度>
-
你没有考虑单元格的重复使用。当您滚动并重用一个单元格时,您会将 view1 添加到一个已经有 view1 的单元格中。
-
您能否发布您对 cellForRowAtIndexPath: 的完整实现以及添加新条目和重新加载表格的方式?
-
我确实在 viewDidAppear 方法中重新加载了表格,好的,我将其发布完成
-
您的问题解决了吗?
标签: ios xcode uitableview addsubview