简体   繁体   中英

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:

- (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? 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. 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.

- (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. 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. 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:

- (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. But either works just as well.

Are you sure channelID is a member of UITableViewCell? If you've subclasesed UITableViewCell and are using your own custom tableview cell, you want to use that class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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