我正在尝试在tableview单元格中加载自定义视图。 确切地说,我有一个对象数组,我想使用自定义视图在单元格中加载内容。 我的对象结构是:
{ ID = 1; date = "2014-01-06"; n1 = 12; n2 = 3; n3 = 34; n4 = 5; n5 = 44; n6 = 24; report = "<null>"; win = 123443; }
我试图在视图中显示n1,n2 ...的值,我制作了这个方法:
@interface MenuCell : UITableViewCell
@property (nonatomic, strong) BallView *ballView;
@property (nonatomic, strong) IBOutlet UILabel *labelDescription;
@property (nonatomic, strong) IBOutlet UILabel *labelDetails;
@property (nonatomic, strong) IBOutlet UIView *ballsView;
+ (CGFloat)getCellHeight;
我的BallView.h
@interface BallView : UIView
@property (nonatomic, strong) IBOutlet UIImageView *imgSelectedBall;
@property (nonatomic, strong) IBOutlet UILabel *labelNo;
@property (nonatomic, strong) IBOutlet UIActivityIndicatorView *spinner;
+ (BallView*)showInView:(UIView*)parrentView xOrigin:(NSInteger)xOriginValue;
@end
我的BallView.m
+ (BallView*)showInView:(UIView*)parrentView xOrigin:(NSInteger)xOriginValue {
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"BallView" owner:self options:nil];
BallView *modal = [nibViews objectAtIndex:0];
modal.frame = CGRectMake(xOriginValue,ball_yOrigin, ball_width, ball_height);
[modal setBackgroundColor:[UIColor greenColor]];
[parrentView addSubview:modal];
return modal; }
在UITableView委托方法的ViewController中,我尝试以这种方式加载信息(我用1号值进行了测试):
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NewsCell";
MenuCell *cell = (MenuCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MenuCell_3" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (MenuCell *) currentObject;
}
}
}
__block TickteObj *obj = [self.dataSourceArray objectAtIndex:indexPath.row];
if (obj){
__block CGFloat leftMark = ball_offSet;
for (int i = 1 ; i<5 ; i++){ //5 = my object has 6 numbers which must be displayed
__block BallView *ballView = [BallView showInView:cell.ballView xOrigin:leftMark];
cell.ballView.labelNo.text = obj.n1;
leftMark+=ball_offSet+ballView.frame.size.width;
}
}
return cell;
}
我需要帮助才能在我的单元格中加载值。谢谢!
答案 0 :(得分:2)
您应该真正更改TickteObj
提供的界面。它不应提供许多属性(n1
,n2
,... nx
),而应提供基于索引的方法:
- (NSString *)nValueAtIndex:(NSInteger)index
并且可能在内部将n值存储在数组中。通过这种方式,无论有多少值都可以,并且您的循环可以轻松运行。