我做了兩個自定義單元格,並顯示在單一的UITableView奇偶命令,但是當我滾動UITableView所有圖像和時間表數據顏色將隨機更改和重複數據將顯示,但名稱,exp。 ,pri-zone和字符串中的速率都顯示正確,只有日程文本的圖像和顏色會發生變化。ios tableview圖像加載時重複滾動tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
static NSString *[email protected]"MyCustomCell";
TableViewCell *cell=[tblResponseData dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:cellIdentifier];
}
NSString *imgPath=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Pic"];
// NSString *imgPath=[[appDelegate.arrData
objectAtIndex:indexPath.row]valueForKey:@"image"];
NSURL *url = [NSURL URLWithString:[imgPath
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *schedule =[[arrData objectAtIndex:indexPath.row]valueForKey:@"Schedule"];
NSArray *strings = [schedule componentsSeparatedByString:@","];
//NSURL *url = [NSURL URLWithString:url];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url]
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse * resp, NSData * data, NSError
* error)
{
if(error == NULL){
// here is our image
UIImage *image = [UIImage imageWithData:data];
cell.imgThumbnail.image=image;
}
else{
cell.imgThumbnail.image=[UIImage imageNamed:@"no preview 2.jpeg"];
}
}];
for (NSString *item in strings)
{
if ([item isEqualToString:@"Su"]) {
cell.lblsu.textColor = [UIColor greenColor];
}else if ([item isEqualToString:@"Tu"]){
cell.lblt.textColor = [UIColor greenColor];
}else if ([item isEqualToString:@"Th"]){
cell.lblth.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"M"]){
cell.lblm.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"F"]){
cell.lblf.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"S"]){
cell.lbls.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"W"]){
cell.lblw.textColor = [UIColor greenColor];
}
}
cell.lblname.text = [[arrData objectAtIndex:indexPath.row] valueForKey:@"Worker Name"];
cell.lblexperience.text=[[arrData o
bjectAtIndex:indexPath.row]valueForKey:@"Experience"];
cell.lblprizone.text=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Primary
Zone"];
cell.lblrate.text=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Rate"];
return cell;
} else {
static NSString *[email protected]"MyCustomCell1";
TableViewCell *cell1=[tblResponseData d
equeueReusableCellWithIdentifier:cellIdentifier1];
if (cell1 == nil)
{
cell1 = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:cellIdentifier1];
}
NSString *imgPath=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Pic"];
// NSString *imgPath=[[appDelegate.arrData
objectAtIndex:indexPath.row]valueForKey:@"image"];
NSURL *url = [NSURL URLWithString:[imgPath
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//NSURL *url = [NSURL URLWithString:url];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url]
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse * resp, NSData * data, NSError
* error)
{
if(error == NULL){
// here is our image
UIImage *image = [UIImage imageWithData:data];
cell1.imgThumbnail.image=image;
}
else{
cell1.imgThumbnail.image=[UIImage imageNamed:@"no preview 2.jpeg"];
}
}];
// NSURL *imageUrl = [NSURL URLWithString:imgPath];
// NSData *imageData = [NSData dataWithContentsOfURL:url];
// UIImage *image=[[UIImage alloc]initWithData:imageData];
// cell.imgThumbnail.image=image;
NSString *schedule =[[arrData objectAtIndex:indexPath.row]valueForKey:@"Schedule"];
NSArray *strings = [schedule componentsSeparatedByString:@","];
for (NSString *item in strings)
{
if ([item isEqualToString:@"Su"]) {
cell1.lblsu.textColor = [UIColor greenColor];
}else if ([item isEqualToString:@"Tu"]){
cell1.lblt.textColor = [UIColor greenColor];
}else if ([item isEqualToString:@"Th"]){
cell1.lblth.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"M"]){
cell1.lblm.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"F"]){
cell1.lblf.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"S"]){
cell1.lbls.textColor = [UIColor greenColor];
}
else if ([item isEqualToString:@"W"]){
cell1.lblw.textColor = [UIColor greenColor];
}
}
cell1.lblname.text = [[arrData objectAtIndex:indexPath.row] valueForKey:@"Worker N
ame"];
cell1.lblexperience.text=[[arrData
objectAtIndex:indexPath.row]valueForKey:@"Experience"];
cell1.lblprizone.text=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Primary
Zone"];
cell1.lblrate.text=[[arrData objectAtIndex:indexPath.row]valueForKey:@"Rate"];
return cell1;
}
}