簡體   English   中英

使用2個不同的對象創建UITableView

[英]Creating a UITableView using 2 different objects

我現在對如何創建UITableView感到困惑。 現在,它可以很好地與1個對象一起為行和單元格提供數據。 問題是,我還有一個第二個對象,我也想在表格視圖中使用。 通常,這非常容易,我只需在UITableView中創建2個部分,並在第一個部分中使用一個對象,然后在第二個部分中使用第二個對象。

但是對於此UITableView,我只想使用1部分。 我需要以編程方式用兩個對象的文本填充行和單元格。 我不知道如何做到這一點,雖然僅使用1個部分。

這是幫助我創建UITableView的所有方法實現:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


     return 1 ;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return [self.messages count];

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {


    return @"Inbox Messages";


}



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


    static NSString *cellIdentifier = @"SettingsCell";

    UITableViewCell  *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];


    PFObject *message = [self.messages objectAtIndex:indexPath.row];


    UIImage *selectMessageButtonImage = [UIImage imageNamed:@"SliderThumb-Normal-G"];


    UIImage *selectMessageButtonImageHighlighted = [UIImage imageNamed:@"SliderThumb-Normal"];



    UIButton *openMessageButton = [[UIButton alloc]init];

    openMessageButton.frame = CGRectMake(237, -10, 64, 64);

    [openMessageButton setImage:selectMessageButtonImage forState:UIControlStateNormal];
    [openMessageButton setImage:selectMessageButtonImageHighlighted forState:UIControlStateHighlighted];
    [openMessageButton setImage:selectMessageButtonImageHighlighted forState:UIControlStateSelected];

    [openMessageButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];



    openMessageButton.tag = indexPath.row;


    [cell.textLabel setText:[message objectForKey:@"senderName"]];

    NSString *fileType = [message objectForKey:@"fileType"];

    if([fileType isEqualToString:@"image"]) {


        cell.imageView.image = [UIImage imageNamed:@"icon_image"];


    } else {

        //no image
    }

    [cell.detailTextLabel setText:@""];

    [cell.contentView addSubview:openMessageButton];


    return cell;

}

我目前與此UITableView一起使用的對象稱為message 我想創建一個名為message2的第二個對象,並將其數據也放置在UITableView中。

我意識到,對於numberOfRowsInSection方法實現,我可以只返回兩個對象的添加計數,但是我不知道如何將第二個對象處理到其余代碼中。

為什么不創建一個結合了兩個msg陣列的NSArray 如果要從某個位置填充第二個味精陣列,只需等待加載完成,然后再執行以下操作:

NSArray *bothArray = [msg1 arrayByAddingObjectsFromArray:msg2];

您可以將bothArray用作TableViewdatasource ,當您只有一個填充的msg數組時,bothArray只是msg1的元素,當您填充msg2 ,只需使用上面的代碼,然后調用:

[self.tableView reloadData];

假設您有2個對象組成的2個數組:

NSArray *msg1;
NSArray *msg2;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return msg1.count + msg2.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   int msg1Count = msg1.count;
   if(indexPath.row < msg1Count)
   { 
      //Here you handle object1
      PFObject *currentObject = msg1[indexPath.row];
   }
   else
    {
       //Here you handle object 2
        PFObject *currentObject = msg2[indexPath.row - msg1Count];
     }
 }

暫無
暫無

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

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