如何在没有cellForRowAtIndexPath的情况下在UIView中填充UITableView

时间:2013-12-05 13:55:20

标签: ios objective-c uitableview uiview uiscrollview

过去几天我正在处理一个问题。我在一个xib文件中有一个UIScrollView,在这个UIScrollView中我把UIViews(也作为xib),在其中UIViews我把UITableViews(也作为xib)。但作为一个新手,我总是使用cellForRowAtindexPath填充我的UITableView,你返回带有你想要的文本的单元格,这一切都完成了,但这次没有调用,所以也许还有另一种方法来填充它?作为我尝试使用CollapseClick作为我的视图的信息。如果有人可以帮助我,或者对如何填充它有任何想法,请随时回答。

这是我的代码,我饶了你所有的xml解析过程。 .m是我初始化我的UITableView

 - (void)viewDidLoad
{
    [super viewDidLoad];
    newsTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewCellStyleDefault];
    newsTable.delegate = self;
    //newsTable.dataSource = self;

    myCollapseClick.CollapseClickDelegate = self;
    [myCollapseClick reloadCollapseClick];

    // If you want a cell open on load, run this method:
    [myCollapseClick openCollapseClickCellAtIndex:0 animated:YES];

    /*
     // If you'd like multiple cells open on load, create an NSArray of NSNumbers
     // with each NSNumber corresponding to the index you'd like to open.
     // - This will open Cells at indexes 0,2 automatically

     NSArray *indexArray = @[[NSNumber numberWithInt:0],[NSNumber numberWithInt:2]];
     [myCollapseClick openCollapseClickCellsWithIndexes:indexArray animated:NO];
     */
}

我需要使用UITable视图返回UIViews

-(UIView *)viewForCollapseClickContentViewAtIndex:(int)index {
    switch (index) {
        case 0:
            return test1View;
            break;
        case 1:
            return test2View;
            break;
        case 2:
            return test3View;
            break;
        default:
            return test1View;
            break;
    }
}

好旧的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];

    CGRect frame = CGRectMake(0, 0, 300, 50);
    UILabel *lbl1 = [[UILabel alloc] initWithFrame:frame];
    lbl1.textAlignment = NSTextAlignmentCenter;
    [lbl1 setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
    //self.tableView.separatorColor = [UIColor whiteColor];
    //lbl1.backgroundColor = [UIColor purpleColor];
    [lbl1 setTextColor:[UIColor blackColor]];
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

    if (storyIndex == 0 && FlagS)
    {
        NSMutableArray * SIR = [[NSUserDefaults standardUserDefaults] objectForKey:@"sizingSheet"];
        NSString * SID = [[SIR objectAtIndex: storyIndex] objectForKey: @"sizing_id"];
        NSString *DID = [[SIR objectAtIndex: storyIndex] objectForKey: @"data_id"];

        Request = [[restRequestManager alloc]init];
        NSString *SDD = [NSString stringWithFormat:@"%@%@%@%@%@", @"sizing_id=", SID, @"&",  @"data_id=", DID];
        NSData* Mimage =  [Request restTestRequesterImage:@"http://www.stackoverflow.com" serviceUri:@"question/Post" parameters:SDD technique:@"POST"];

        cell.imageView.image = [UIImage imageWithData:Mimage];
        return cell;
    }

    searchString = @"TH_MEASURE_CATEGORY_";
    str=[category objectAtIndex:storyIndex];
    NSArray *array = [[NSMutableArray alloc]init];
    array = [str componentsSeparatedByString:@"@"];

    for (NSString *tempStr in array) {
        NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
        if (result == NSOrderedSame) {

            lbl1.text = [category objectAtIndex: storyIndex];
            lbl1.text = [lbl1.text substringFromIndex: MIN(20, [lbl1.text length])];
            [cell.contentView addSubview:lbl1];
        }
        else{
            lbl1.text = [category objectAtIndex: storyIndex];
            [cell.contentView addSubview:lbl1];
        }
    }
    return cell;

}

和.h没有所有无用的东西

@interface MeasuresViewController: UIViewController <CollapseClickDelegate,UITextFieldDelegate, UITableViewDelegate, UITableViewDelegate> {

    IBOutlet UIView *test1View;
    IBOutlet UIView *test2View;
    IBOutlet UIView *test3View;

    __weak IBOutlet CollapseClick *myCollapseClick;

    IBOutlet UITableView * newsTable;

    BOOL *FlagS;

    UIActivityIndicatorView * activityIndicator;

    CGSize cellSize;


    NSMutableArray * stories;

    NSMutableString * currentCategory, * currentSubcategory, * currentValue, * currentName, * currentSrc;

    NSDictionary *data, *data1, *data2, *data3, *data4;


}

- (void)parseXMLFileAtURL:(NSString *)URL;
-(BOOL)ifStringExists:(NSString *)stringSentToCheck selectYourCheckArray:(NSMutableArray *)SelectedArray;
@property (nonatomic, strong) NSMutableArray *photos;
@property (atomic, strong) NSMutableArray *assets;

@end

1 个答案:

答案 0 :(得分:1)

您注释掉了设置数据源的行。如果未设置数据源,则不会调用cellForRowAtIndexPath:。

您当前的viewDidLoad代码是:

- (void)viewDidLoad {
    [super viewDidLoad];
    newsTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewCellStyleDefault];
    newsTable.delegate = self;
    //newsTable.dataSource = self;

    myCollapseClick.CollapseClickDelegate = self;
    [myCollapseClick reloadCollapseClick];

    // If you want a cell open on load, run this method:
    [myCollapseClick openCollapseClickCellAtIndex:0 animated:YES];

    /*
     // If you'd like multiple cells open on load, create an NSArray of NSNumbers
     // with each NSNumber corresponding to the index you'd like to open.
     // - This will open Cells at indexes 0,2 automatically

     NSArray *indexArray = @[[NSNumber numberWithInt:0],[NSNumber numberWithInt:2]];
     [myCollapseClick openCollapseClickCellsWithIndexes:indexArray animated:NO];
     */
}

将其更改为

 - (void)viewDidLoad
{
    [super viewDidLoad];
    newsTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewCellStyleDefault];
    newsTable.delegate = self;
    newsTable.dataSource = self;

    myCollapseClick.CollapseClickDelegate = self;
    [myCollapseClick reloadCollapseClick];

    // If you want a cell open on load, run this method:
    [myCollapseClick openCollapseClickCellAtIndex:0 animated:YES];

    /*
     // If you'd like multiple cells open on load, create an NSArray of NSNumbers
     // with each NSNumber corresponding to the index you'd like to open.
     // - This will open Cells at indexes 0,2 automatically

     NSArray *indexArray = @[[NSNumber numberWithInt:0],[NSNumber numberWithInt:2]];
     [myCollapseClick openCollapseClickCellsWithIndexes:indexArray animated:NO];
     */
}