开发者

Re-Using UITableViewCell in a function which is not UITableViewDelegate

I have a different problem.

I am creating my TableViewCells like:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    UILabel *label = nil;

    NSString *cellIdentifier = [NSString stringWithFormat:@"Cell_%i", indexPath.row];

    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
        //cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier];
        label = [[UILabel alloc] initWithFrame:CGRectZero];
        [label setLineBreakMode:UILineBreakModeWordWrap];
        [label setMinimumFontSize:FONT_SIZE];
        [label setNumberOfLines:0];
        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [label setTag:1];


        [[cell contentView] addSubview:label];

    }
return cell;
}

as you see, i have cells with identifiers like "Cell_0 , Cell_1, etc"

and in a function which is not a TableView Method, i want to use this cells by calling their identifiers. i do this like:


UITableViewCell *cell  = [myTableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell_%i", myCellID]];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:@"Cell_%i", myCellID]];
    }
UILabel *label = (UILabel*)[cell viewWithTag:1];

cant i use lik开发者_Python百科e this?

i am trying to change the color of label in the cell i try to access, but i cant. cell is not nil, but label is always nil. what should i do?

how can i understand if its the cell i want or an empty cell?


NSString *cellIdentifier = [NSString stringWithFormat:@"Cell_%i", indexPath.row];

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

This defeats the whole purpose of cell reuse!

You should use the same cell identifier for all cells that "look" the same (i.e. have the same subviews), and only change their contents.

To get a cell you created for a particular row, use [tableView cellForRowAtIndexPath:indexPath].


ok i have solved my problem by filling all indexPaths in an array and while calling a cell, calling the index path from that array. thanks for answers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜