簡體   English   中英

uitableview對象數組作為數據源

[英]uitableview array of objects as datasource

我在ios xcode項目的情節提要中有一個UITableView,

我也有一個menuitem對象數組存儲在itemarray

NSMutableArray *itemarray;

這是我的menuitem.h文件

#import <Foundation/Foundation.h>
@interface menuitem : NSObject
@property (nonatomic) NSString *itemtitle;
@property (nonatomic) NSString  *itemdesc;
@property (nonatomic) int itemid;
@property (nonatomic) int itemprice;
@property (nonatomic) NSString  *itemcat;
@end

這是我的tableviewcell.m文件:

#import "tableviewcell.h"
#import "menuitem.h"
@interface tableviewcell ()
@property (nonatomic, weak) IBOutlet UILabel *titleLabel;
@property (nonatomic, weak) IBOutlet UILabel *descLabel;
@property (nonatomic, weak) IBOutlet UILabel *priceLabel;


@end

@implementation tableviewcell
-(void)configureWithmenuitem:(menuitem *)item{
    NSLog(@"hello");

    self.titleLabel.text = item.itemtitle;
    self.descLabel.text = item.itemdesc;

    self.priceLabel.text = [NSString stringWithFormat:@"%.1d", item.itemprice];
}

@end

我想從menuitem每個實例獲取itemtitleitemdescitemprice到每個tableviewcell的3個標簽中

您的tableviewcell是一個View,因此它應該只知道如何顯示自身而不顯示什么。 控制器的工作(通過委托tableView:cellForRowAtIndexPath:方法)告訴視圖顯示什么。

因此,您應該執行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Id"];

      //Assuming you've stored your items in an array.
      Item *item = [items objectAtIndex:indexPath.row];

      cell.titleLabel.text = item.itemTitle;
      cell.descLabel.text = item.itemdesc;
      cell.priceLabel.text = [NSString stringWithFormat:@"%.1d", item.itemprice];
      return cell;
}

^上面的代碼假定您具有原型單元。 如果不這樣做,則在出隊后,如果cell為nil,則必須分配。

暫無
暫無

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

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