0

我有一个带有自定义单元格(下面的代码)的 UITableView,它从NSDictionaryNSDictionary从 NSArray 加载数据)加载数据objectatIndexPath:indexpath.row

现在,我有一个以相同方式工作的 DidSelectRow 函数(NSArray -> NSDictionary -> Cell 数据)。

问题是当我滚动我的表格时,它会改变我的单元格。我必须让我的单元格始终重新加载相同的订单。我必须在滚动时停止重新加载数据。

这是我用来加载我的表格视图并将数据加载到表格视图单元格和我的 didSelectRow 的代码:请帮助,我没有时间解决这个问题......

        RequestArray = [jsonDictionary objectForKey:@"Content"];

        RequestTable =  [[UITableView alloc]initWithFrame:CGRectMake(0, 84, 320, 326)];
        RequestTable.backgroundColor = [UIColor colorWithRed:(234/255.f) green:(234/255.f) blue:(234/255.f) alpha:1];

        RequestTable.dataSource=self;
        RequestTable.delegate=self;

        RequestTable.rowHeight = 77;
        [self.view addSubview:RequestTable];

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];

        NSDictionary *object = [RequestArray objectAtIndex:indexPath.row];

        NSString *Category = [object valueForKey:@"Category"];
        NSString *ItemDescription = [object valueForKey:@"ItemDescription"];
        NSString *ItemName = [object valueForKey:@"ItemName"];
        NSString *Date = [object valueForKey:@"Date"];
        NSString *ExpiresDate = [object valueForKey:@"ExpiresDate"];
        BOOL isRead = [[object valueForKey:@"IsRead"]boolValue];


        /// add time label to cell
        NSTimeInterval TIMEinterval = [Date intValue];
        NSDate* TIMEDate = [NSDate dateWithTimeIntervalSince1970:TIMEinterval];
        NSDateFormatter* df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"HH:mm"];
        [df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:2]]; //+0000
        NSString* dateStrig = [df stringFromDate:TIMEDate];

        UILabel *RequestTime = [[UILabel alloc]initWithFrame:CGRectMake(268, 8, 33, 16)];
        [RequestTime setTextAlignment:UITextAlignmentRight];
        RequestTime.backgroundColor = [UIColor clearColor];
        RequestTime.textColor = [UIColor blackColor];
        [RequestTime setFont:[UIFont systemFontOfSize:12]];
        RequestTime.text = dateStrig;
        [cell addSubview:RequestTime];

        /// add experation label to cell

        NSTimeInterval EXPTIMEinterval = [ExpiresDate intValue];
        NSDate* EXPDate = [NSDate dateWithTimeIntervalSince1970:EXPTIMEinterval];

        NSTimeInterval secondsBetween = [EXPDate timeIntervalSinceDate:TIMEDate];
        int numberOfHours = secondsBetween / 3600;

        UILabel *ExperationTime = [[UILabel alloc]initWithFrame:CGRectMake(152, 24, 150, 15)];
        ExperationTime.backgroundColor = [UIColor clearColor];
        [ExperationTime setTextAlignment:UITextAlignmentRight];
        ExperationTime.textColor = [UIColor colorWithRed:(255/255.f) green:(103/255.f) blue:(18/255.f) alpha:1];
        [ExperationTime setFont:[UIFont systemFontOfSize:14]];
        if (numberOfHours>24){
            numberOfHours = numberOfHours/24;
            ExperationTime.text = [NSString stringWithFormat:@"המוצר דרוש תוך %d ימים",numberOfHours];

        }else{
            ExperationTime.text = [NSString stringWithFormat:@"המוצר דרוש תוך %d שעות",numberOfHours];

        }
        [cell addSubview:ExperationTime];

        // add item category to cell

        UILabel *itamCategory = [[UILabel alloc]initWithFrame:CGRectMake(143 , 5, 120, 21)];
        itamCategory.backgroundColor = [UIColor clearColor];
        [itamCategory setTextAlignment:UITextAlignmentRight];
        itamCategory.textColor = [UIColor colorWithRed:(0/255.f) green:(177/255.f) blue:(233/255.f) alpha:1];
        [itamCategory setFont:[UIFont boldSystemFontOfSize:17]];
        itamCategory.text = Category;
        [cell addSubview:itamCategory];

        // add item Name to cell

        UILabel *itamName = [[UILabel alloc]initWithFrame:CGRectMake(98, 39, 203, 21)];
        itamName.backgroundColor = [UIColor clearColor];
        [itamName setTextAlignment:UITextAlignmentRight];
        itamName.textColor = [UIColor blackColor];
        [itamName setFont:[UIFont boldSystemFontOfSize:17]];
        itamName.text = ItemName;
        [cell addSubview:itamName];

        // add item Description to cell

        UILabel *Description = [[UILabel alloc]initWithFrame:CGRectMake(98, 62, 203, 10)];
        Description.backgroundColor = [UIColor clearColor];
        [Description setTextAlignment:UITextAlignmentRight];
        Description.textColor = [UIColor blackColor];
        [Description setFont:[UIFont systemFontOfSize:13]];
        Description.text =ItemDescription;
        [cell addSubview:Description];

        //add sendOffer button
        UIButton *callSeller = [UIButton buttonWithType:UIButtonTypeCustom];
        callSeller.frame = CGRectMake(25, 8, 54, 45);
        [callSeller setTag:indexPath.row];

        [callSeller setBackgroundImage:[UIImage imageNamed:@"makeOfferTable.png"] forState:UIControlStateNormal];
        [callSeller setBackgroundImage:[UIImage imageNamed:@"makeOfferTable.png"] forState:UIControlStateHighlighted];

        [callSeller addTarget:self action:@selector(makeaOffer:) forControlEvents:UIControlEventAllEvents];
        [cell addSubview:callSeller];

        UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
        myBackView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"highlightCellBG.png"]];
        cell.selectedBackgroundView = myBackView;
        cell.backgroundColor = [UIColor lightGrayColor];

        //add newOffer sign (if new)
        if (!isRead){
            UIImageView *newIndocator = [[UIImageView alloc]initWithFrame:CGRectMake(295, 0, 25, 25)];
            newIndocator.image = [UIImage imageNamed:@"newOfferIndicator.png"];
            [newIndocator setContentMode:UIViewContentModeScaleToFill];
            [cell addSubview:newIndocator];
        }

    }

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            cell.accessoryType = UITableViewCellAccessoryNone;
    }


    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        NSDictionary *object = [RequestArray objectAtIndex:indexPath.row];
        NSLog(@"indePath.row = %d",indexPath.row);

        Seller_RequestDetailsViewController *infoView = [[Seller_RequestDetailsViewController alloc]initWithNibName:@"Seller_RequestDetailsViewController" bundle:nil];

        NSString *OfferDate = [object valueForKey:@"Date"];

        NSTimeInterval TIMEinterval = [OfferDate intValue];
        NSDate* TIMEDate = [NSDate dateWithTimeIntervalSince1970:TIMEinterval];
        NSDateFormatter* df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"DD/MM/yyyy"];
        [df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:2]]; //+0000

        infoView.date = [df stringFromDate:TIMEDate];


        infoView.itemName = [object valueForKey:@"ItemName"];
        infoView.modalName = [object valueForKey:@"ItemDegem"];
        infoView.buyerID = [NSString stringWithFormat:@"%@",[object valueForKey:@"BuyerUserID"]];
        infoView.city = [object valueForKey:@"Region"];
        infoView.Offerdescription = [object valueForKey:@"ItemDescription"];
        BOOL isRead = [[object valueForKey:@"IsRead"]boolValue];

        infoView.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self presentModalViewController:infoView animated:YES];

        if (!isRead){
            [self markOfferAsRead:[NSString stringWithFormat:@"%@",[object valueForKey:@"ItemID"]]];
        }        

}
4

2 回答 2

0

your problem is that everything is with in this:

if(cell == nil) {
    //Your Code
}

What is happening is the tableview reuses cells. You are only changing the content if(cell == nil). What you need is to only create a new cell in side if(cell == nil) and move the rest of your code out of that if statement

于 2012-05-09T23:51:48.290 回答
0

我不确定我是否完全理解您的问题,但我确实看到您构建单元的方式存在错误。将单元格出列后,您应该只在它为 nil 时才初始化单元格,而不是初始化并构建单元格。

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
}

// Now build the cell.

如果您不每次都重建单元格,它只会重用最初构建的单元格。

于 2012-05-09T18:41:34.857 回答