【问题标题】:How to populate a UITableView in a UIView without cellForRowAtIndexPath如何在没有 cellForRowAtIndexPath 的 UIView 中填充 UITableView
【发布时间】:2013-12-05 13:55:19
【问题描述】:

过去几天我正在处理一个问题。我在 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

【问题讨论】:

  • 你设置了tableview委托和数据源方法吗??或在xib中设置链接。
  • 代表像您在 .h 中声明的代表?我现在用 .h 编辑。
  • //newsTable.dataSource = self; ???删除评论
  • 几秒钟前做的,没有任何反应仍然忽略它,我收到一个警告,它说从不兼容的类型“MeasuresViewController“const_strong”分配给“id”。是否必须在 UITableViewController 类中声明 cellForRowAtIndexPath 才能工作?

标签: ios objective-c uitableview uiview uiscrollview


【解决方案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];
     */
}

【讨论】:

  • 对不起我的笨拙,但是什么是数据源?
  • dataSource 告诉 UITableView 数据(单元格)来自哪里。在您的问题中,您有 view did load 方法:[super viewDidLoad]; newsTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewCellStyleDefault]; newsTable.delegate = self; //newsTable.dataSource = self;只需更改最后一行,使其不会被注释掉,并且应该调用 cellForRowAtIndexPath。
  • 谢谢你的回答,我已经取消注释了,在cellForRowAtIndexPath中放了一个小断点,编译,运行,但是没有进入cellForRowAtIndexPath,就像不存在一样。
  • 我刚刚重新阅读了您的问题。你说表格视图是在xib中创建的?如果是这样,您在 viewDidLoad 中创建的 newsTable 是否有任何作用?如果表是在 xib 中创建的,您可以右键单击它,然后在弹出窗口中有一个 dataSource 选项。确保 dataSource 连接到“文件的所有者”
  • 是的,它在 xib 中被调用,是的,newsTable 是我的 UIView 中的 UITableView。确实它没有连接到数据源:3,但我得到一个异常:3。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多