簡體   English   中英

目標c中的UITableView中的CustomCell

[英]CustomCell in UITableView in objective c

在此處輸入圖片說明

我在這里附加了一個示例輸出...。它有兩個單元格,第一個具有功能代碼,開始時間,結束時間,第二個具有中斷時間。

現在我想做的是第三個應該包含functionCode:,starttime:,endtime :,

我的數組有3個字典

(
        {
        date = "2016-01-20";
        "end_time" = "11:10:00";
        "function_code" = RCV;
        "operator_id" = JOHN;
        "start_time" = "11:00:00";
        "total_time" = 10;
        "total_units" = 19;
    },
        {
        "break_time" = 65;
    },
        {
        date = "2016-01-20";
        "end_time" = "12:25:00";
        "function_code" = PIK;
        "operator_id" = JOHN;
        "start_time" = "12:15:00";
        "total_time" = 10;
        "total_units" = 26;
    } )

這是我在indexpath方法中的cellforrow

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

   if (indexPath.row ==0)
    {

        self.tempDictionary = [self.arrData1 objectAtIndex:indexPath.row];
        NSLog(@"The dictionary contains:%@",self.tempDictionary);


        static NSString *CellIndentifier =@"TableViewCell";
        TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIndentifier];

        if (cell == nil) {
            cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];

       }

        NSLog(@" the dictionary contains all :%@",self.tempDictionary);


        NSString *strFunctionCodeValue =[self.tempDictionary objectForKey:@"function_code"];
        NSString *strStartTimeValue = [self.tempDictionary objectForKey:@"start_time"];
        NSString *strEndTimeValue = [self.tempDictionary objectForKey:@"end_time"];
        NSString *strTotalUnitsValue =[self.tempDictionary objectForKey:@"total_units"];
        NSString *strTotalTimeValue = [self.tempDictionary objectForKey:@"total_time"];


        cell.lblFunctionCodeValue.text = strFunctionCodeValue;
        cell.lblStartTimeValue.text = strStartTimeValue;
        cell.lblEndTimeValue.text = strEndTimeValue;
        cell.lblTotalUnitsValue.text =strTotalUnitsValue;
        cell.lblTotalTimeValue.text = strTotalTimeValue;

        return cell;

    }
    else
    {

        static NSString *cellIdentifier1 =@"TableViewCell1";
        self.tempDictionary = [self.arrData1 objectAtIndex:indexPath.row];
        NSLog(@" dicitionary :%@",self.tempDictionary);

        TableViewCell1 *cell1 =(TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        if (cell1 == nil) {
            cell1 =[[TableViewCell1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];

      }

        NSString *breakTimeValue = [self.tempDictionary objectForKey:@"break_time"];
        cell1.lblBreakTimeValue.text = breakTimeValue;

        return cell1;

    }

}

使名為MyObject的類繼承自NSObject。 它將包含以下屬性date,end_time,function_code,operator_id,start_time,total_time,total_units。

然后,您將使另一個類MyObject2僅包含一個屬性** break_time **。

您將通過添加檢查字典上的所有鍵是否大於1來將字典解析為相應的模型,這意味着您必須解析myObject1中的數據,否則將其解析為myObject2。 您將在數組中添加這些解析的對象,這些對象可以進一步用作uitableview的數據源。 現在,在tableview cellForRowAtIndexPath的委托方法中,您所需要做的只是實現檢查。

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

     id *object = [array objectAtIndex:indexPath.row];
     if([object isKindOfClass:MyObject]){
         CustomCell1 *cell1 = (CustomCell1 *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell1"];
         return cell1;
     }else{
        CustomCell2 *cell2 = (CustomCell2 *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell2"];
         return cell2;
     }
     return;
}

在現有代碼上,您可以添加支票。

self.tempDictionary = [self.arrData1 objectAtIndex:indexPath.row];
if([[self.tempDictionary allKeys] count]>1){
         static NSString *CellIndentifier =@"TableViewCell";
    TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIndentifier];

    if (cell == nil) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];

    }

    NSLog(@" the dictionary contains all :%@",self.tempDictionary);

    NSString *strFunctionCodeValue =[self.tempDictionary objectForKey:@"function_code"];
    NSString *strStartTimeValue = [self.tempDictionary objectForKey:@"start_time"];
    NSString *strEndTimeValue = [self.tempDictionary objectForKey:@"end_time"];
    NSString *strTotalUnitsValue =[self.tempDictionary objectForKey:@"total_units"];
    NSString *strTotalTimeValue = [self.tempDictionary objectForKey:@"total_time"];


    cell.lblFunctionCodeValue.text = strFunctionCodeValue;
    cell.lblStartTimeValue.text = strStartTimeValue;
    cell.lblEndTimeValue.text = strEndTimeValue;
    cell.lblTotalUnitsValue.text =strTotalUnitsValue;
    cell.lblTotalTimeValue.text = strTotalTimeValue;


}else{
    static NSString *cellIdentifier1 =@"TableViewCell1";
    self.tempDictionary = [self.arrData1 objectAtIndex:indexPath.row];
    NSLog(@" dicitionary :%@",self.tempDictionary);

    TableViewCell1 *cell1 =(TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
    if (cell1 == nil) {
        cell1 =[[TableViewCell1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];

 }


    NSString *breakTimeValue = [self.tempDictionary objectForKey:@"break_time"];

    cell1.lblBreakTimeValue.text = breakTimeValue;


    return cell1;
}

有什么問題

可能需要構建2個不同的單元格,並根據某些數據在cellForRow中返回一個或另一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM