简体   繁体   English

iOS TableView如何访问自定义单元格变量

[英]iOS TableView how to access custom cell variables

I have a table view 我有一个表视图

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    **NSString *channelID = [object objectForKey:@"channelID"];**

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell==nil)
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle 
                                     reuseIdentifier:CellIdentifier];

}

I'm accessing a tableview cell like so: 我正在访问一个tableview单元格,如下所示:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    for (NSIndexPath *path in [tableView indexPathsForVisibleRows]) {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
    }
    //I want to access cell.ChannelID //
}

How can I access the channelID variable? 如何访问channelID变量? When the user stops scrolling I want to access the cells visible and a custom variable. 当用户停止滚动时,我想访问可见的单元格和自定义变量。

Thanks 谢谢

If you want a custom UITableView cell, then you need to make a subclass of UITableView cell. 如果需要自定义UITableView单元,则需要创建UITableView单元的子类。 From there, you need to declare a property on this custom cell for your object. 从那里,您需要在此自定义单元格上为您的对象声明一个属性。 Make sure that this property is declared publicly. 确保公开声明此属性。 From what I've seen in your code, it should probably be something like this. 从我在你的代码中看到的,它可能应该是这样的。

@interface MyCustomCell : UITableViewCell

@property (strong, nonatomic) NSDictionary *myObject;

@end

From there, you'll want to import the header of the cell subclass into the implementation file that your table view delegate/datasource methods uses, and instead of referencing UITableViewCell inside cellForRowAtIndexPath: reference your subclass, then set a value to your newly added property. 从那里,您将要将单元子类的标头导入到表视图委托/数据源方法使用的实现文件中,而不是在cellForRowAtIndexPath中引用UITableViewCell:引用您的子类,然后将值设置为新添加的属性。

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

    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSString *channelID = [cell.myObject objectForKey:@"channelID"];

    [cell.textLabel setText:channelID];

    return cell;
}

Of course it should go without saying that you'll first need to provide logic to propagate this dictionary. 当然,不用说你首先需要提供逻辑来传播这个字典。 Then as far as the scroll view delegate method goes, you have a similar problem. 然后,就滚动视图委托方法而言,您有类似的问题。 You can't access a custom object on the UITableViewCell base class. 您无法访问UITableViewCell基类上的自定义对象。 You once again need to reference your subclass. 您再次需要引用您的子类。 This can be easily done here with a simple cast. 这可以通过简单的演员在这里轻松完成。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    for (NSIndexPath *path in [tableView indexPathsForVisibleRows]) {
        MyCustomCell *cell = (MyCustomCell *)[self.tableView cellForRowAtIndexPath:path];

        NSString *channelID = [cell.myObject objectForKey:@"channelID"];
        //
    }
}

0x7fffffff answer works. 0x7fffffff答案有效。 You can also use this function and skip the null check. 您也可以使用此功能并跳过空检查。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];

    NSString *channelID = [cell.myObject objectForKey:@"channelID"];

    [cell.textLabel setText:channelID];
}

But you must declare what a "CustomCell" is by registering it in viewDidLoad or you will get an exception: 但是你必须通过在viewDidLoad中注册它来声明“CustomCell”是什么,否则你将得到一个例外:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
}

I like this better because it makes the dequeueReusableCellWithIdentifier: forIndexPath: is guaranteed to return a cell and makes cellForRowAtIndexPath a little cleaner. 我更喜欢这个,因为它使dequeueReusableCellWithIdentifier: forIndexPath:保证返回一个单元格并使cellForRowAtIndexPath更清洁。 But either works just as well. 但要么也适用。

Are you sure channelID is a member of UITableViewCell? 你确定channelID是UITableViewCell的成员吗? If you've subclasesed UITableViewCell and are using your own custom tableview cell, you want to use that class 如果您已经创建了UITableViewCell并使用自己的自定义tableview单元格,则需要使用该类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM