开发者

Can't get texts on UITableViewCell

I want to get multiple custom texts on cell of UITableView. I want these texts after clicking on a dynamically added button on the cell. I can't use didRowSelectedAtIndex method. I have my own selector w开发者_运维技巧hich I am executing but I don't know how can I get texts on each cell. Anybody can help me?

Thank


You add a UILabel to the cell's view. Just like you did it with your custom Button.

You can add a UILabel to all cells in advance. If the UIButton on a cell is cklicked, you have to save that state information somewhere (NSArray). When it comes to rendering the cell, you ask the array if the cell at current index should show some text and set it up if so...

If you want immediate rendering call [self.tableView reloadData]

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (array at index indicates clicked) {
        myCustomCell.label1 = @"foo";
    } else {
        myCustomCell.label1 = @"bar";
    }
}

If you don't like customs cells. You can apply a tag to the label, add the label to the cell and get it from cell by querying it afterwards by that tag.


  1. Make your labels as global variables
  2. Create yur cell as default style cell.
  3. Add your labels to contentView of cell without text.
  4. Add your button.
  5. In the method that button invokes after click set texts to your labels.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
    customLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 50, 20)];
    customLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(55, 5, 50, 20)];
    [cell.contentView addSubview:customLabel1];
    [cell.contentView addSubview:customLabel2];
    }
    

and

    - (IBAction) click
    {
        customLabel1.text = @"YAYA";
        customLabel2.text = @"GAGA";
    }


You could maybe try creating a custom button class by extending UIButton and add properties to contain references to the NSString objects (or index of the original array where the strings are stored etc).

Then on button click you can cast the sender to your custom button type and simply retrieve the property values.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜