Ios NSObject数据到UITableViewCell
我创建了一个具有一些属性的类。在故事板中,我有一个tableview控制器。我想将数据从类传递到tableview。在每个单元格中我都有一些标签。我想填充数据,但数组没有成功。你能帮帮我吗 所以我有一个City类,它有一些属性,比如property1,property2,它们是标签Ios NSObject数据到UITableViewCell,ios,objective-c,uitableview,nsobject,Ios,Objective C,Uitableview,Nsobject,我创建了一个具有一些属性的类。在故事板中,我有一个tableview控制器。我想将数据从类传递到tableview。在每个单元格中我都有一些标签。我想填充数据,但数组没有成功。你能帮帮我吗 所以我有一个City类,它有一些属性,比如property1,property2,它们是标签 #import <Foundation/Foundation.h> @interface DACityObject : NSObject { NSNumber *ID; NSS
#import <Foundation/Foundation.h>
@interface DACityObject : NSObject {
NSNumber *ID;
NSString *name;
NSNumber *temperature;
NSNumber *minimumTemperature;
NSNumber *maximumTemperature;
NSString *iconName;
}
@property (strong,nonatomic) NSNumber *ID;
@property (strong,nonatomic) NSString *name;
@property (strong,nonatomic) NSNumber *temperature;
@property (strong,nonatomic) NSNumber *minimumTemperature;
@property (strong,nonatomic) NSNumber *maximumTemperature;
@property (strong,nonatomic) NSString *iconName;
@end
达契塞尔
#import <UIKit/UIKit.h>
@interface DACityCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UILabel *temperature;
@property (strong, nonatomic) IBOutlet UILabel *minimumTemperature;
@property (strong, nonatomic) IBOutlet UILabel *maximumTemperature;
@property (strong, nonatomic) IBOutlet UIImageView *iconName;
@end
如何创建例如3个城市的数据并将其放在我的主控制器的表视图中?假设您已经通过类对象
DACityObject创建了3个数据
,现在只需将它们放在一个数组中,比如说dataArray(NSMutableArray)
并在具有UITableView
的类中的方法cellforrowatinexpath
中调用它们,然后只需调用自定义tableview单元格即可
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
// extract data here
DACityObject *dataObject = [dataArray objectAtIndex:indexPath.row];
DACityCell *cell = (DACityCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DACityCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = dataObject.name;
cell.temperature.text = dataObject.temperature;
.
.
.... etc.
return cell;
}
假设您已经通过class对象
DACityObject
创建了3个数据,现在需要做的就是将它们放置在一个数组中,比如dataArray(NSMutableArray)
,并在具有UITableView
的类中的方法cellforrowatinexpath
中调用它们,然后只需调用自定义tableview单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
// extract data here
DACityObject *dataObject = [dataArray objectAtIndex:indexPath.row];
DACityCell *cell = (DACityCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DACityCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = dataObject.name;
cell.temperature.text = dataObject.temperature;
.
.
.... etc.
return cell;
}
为什么要在两个类中重复这些属性?更好的方法是将
DACityObject
传递到单元格,并说“这里,使用此数据”。由于我是新手,您能给我更清楚的答案吗:)为什么在两个类中都复制这些属性?最好是将DACityObject
传递到单元格并说“这里,使用此数据”。由于我是新手,您能给我更清楚的答案吗:)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
// extract data here
DACityObject *dataObject = [dataArray objectAtIndex:indexPath.row];
DACityCell *cell = (DACityCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DACityCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = dataObject.name;
cell.temperature.text = dataObject.temperature;
.
.
.... etc.
return cell;
}