我在 ios xcode 项目的 Storyboard中有一个 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
的每个实例中的 itemtitle
、itemdesc
和 itemprice
获取到 3 个标签中每个 tableviewcell
最佳答案
您的 tableviewcell 是一个 View ,因此它应该只知道如何显示自己而不是显示什么。告诉 View 显示什么是 Controller 的工作(通过委托(delegate) 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;
}
^ 上面的代码假定您有一个原型(prototype)单元格。如果不这样做,则如果单元格在出队后为 nil,则必须进行分配。
关于ios - uitableview 对象数组作为数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23948427/