![](https://img.haomeiwen.com/i3627637/775180b553353b77.png)
*这个主要的关键点是如何分别记录每个textView输入的内容和评论的星星数,并和商品一一对应 传给后台
这里是针对购物车一家店选中并购买多个商品,订单号是生成一个,我得订单里面还是一对多得时候,评价就需要把商品分开,一一对应的评价.
首先应该应该使用TableView来布局,因为需要评价的商品的个数是不确定的,这里我们用XIB来自定义cell,很多操作和赋值 ,我们就在cell中来完成
*这里是cell里面的一些数据处理
@interface CarJudgeTableViewCell : UITableViewCell<RatingBarDelegate,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet RatingBarView *rating;
@property (weak, nonatomic) IBOutlet UILabel *plander;
@property (copy, nonatomic) void(^plusBlock) (NSString *countStr,NSString *star,NSInteger type);
这是.h里面的一些控件和属性,星星我用的是RatingBarView,不知道的可以去搜一下,这里用的的是BLOCK回调,在Controller里面要用
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[_rating setImageDeselected:@"star2"
halfSelected:@"starB"
fullSelected:@"rating_show"
andDelegate:self];
_rating.isBig = YES;
_textView.delegate = self;
_textView.layer.masksToBounds = YES;
_textView.layer.cornerRadius = 5;
[_textView.layer setBorderColor:UICOLOR_HEX(0xf5f5f5).CGColor];
[_textView.layer setBorderWidth:0.8];
self.selectionStyle = UITableViewCellAccessoryNone;
}
在awakeFromNib方法里面设置textView的边框和圆角 及代理方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
self.plander.text = @"";
return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
_plusBlock(_textView.text,star,0);
return YES;
}
当获取焦点的时候把textView的占位灰色字给清除,然后再编辑结束后用_plusBlock(_textView.text,star,0);来把输入的内容给传出去
//下面是选中星星的时候的代理,同样把数据给传出去
- (void)ratingChanged:(float)newRating
{
star = [NSString stringWithFormat:@"%.1f",newRating];
_plusBlock(_textView.text,star,1);
}
下面是Controller里面的数据处理了
首先处理是在cellForRowAtIndexPath里面处理的
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
CarJudgeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarJudgeTableViewCell11" forIndexPath:indexPath]; cell.plusBlock = ^(NSString *count,NSString *star,NSInteger type) { __block int isHave = 0; [selectArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj[@"row"] isEqualToString:[NSString stringWithFormat:@"%ld",(long)indexPath.section]]) { [selectArr removeObject:obj]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:count forKey:@"count"]; [dic setObject:star forKey:@"star"]; [dic setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.section] forKey:@"row"]; [selectArr addObject:dic]; isHave = 1; //数组升序排列 NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc] initWithKey:@"row" ascending:YES]; NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:&sortDes1 count:1]; [selectArr sortUsingDescriptors:sortDescriptors]; return; } }]; if (isHave == 0) { NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:count forKey:@"count"]; [dic setObject:star forKey:@"star"]; [dic setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.section] forKey:@"row"]; [selectArr addObject:dic]; //数组升序排列 NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc] initWithKey:@"row" ascending:YES]; NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:&sortDes1 count:1]; [selectArr sortUsingDescriptors:sortDescriptors]; } NSLog(@"输入了什么?===%@",selectArr); return;
};
下面来解析一下:
1.selectArr是一个可变数组,负责存储已填写的星星和内容 的字典给存起来,存得时候首先遍历数组,看看数组中有没有当前要输入的索引,我用indexPath.section来记录, 如果有得话,就把原来的删除掉[selectArr removeObject:obj]; 这句代码 ,删除以后 把新修改的数据给保存起来,然后数组重排一下, PS:方便查看,看着舒服! 大家应该注意到了,我在遍历前__block int isHave = 0; 这里有得话isHave = 1; 置为1 ,至于为什么,下面我会讲解
2.也就是当isHave = 0;的时候,这说明要不数组里面没数据,要不就是你选中的cell上面的内容还没有存,这种情况直接存就好了
下面我们来输入内容
![](https://img.haomeiwen.com/i3627637/25c8f86537a7872d.png)
我们打印一下selectArr的数据:
(
{
count = Ttttttttttttttttttttttttttttt;
row = 0;
star = "2.0";
},
{
count = Pppppppppppppppppppppppp;
row = 1;
star = "3.0";
},
{
count = Bbbbbbbbbbbbbbbbbbbbbbbb;
row = 2;
star = "5.0";
}
)
网友评论