How to get UITableView Label Text string - Custom Cell

北战南征 提交于 2020-01-14 11:46:07

问题


I have a UITableView with CustomCell. The custom cells contain a UILabel and a UIImageView.

I saw online that it is possible to get the text from a normal UITableView cell and store it in a string when the user presses the cell.

But how can you do this when you are using a custom cell? Becuase the name of my UILabel is "type_label" and it has been defined in the header file of my CustomCell XIB.

So in Xcode I can't use:

cell.type_label.text"

In the following function:

-(void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Thanks, Dan.


回答1:


In the tableViewDidSelectRow all you need to do is:

UITableViewCellCustomClass *customCell = (UITableViewVellCustomClass*)[tableView cellForRowAtIndexPath:indexPath];

NSString *labelText = [[customCell type_label] text];

That should get you want you need.




回答2:


Try below code - You have create type_label label's IBOutlet in CCustomCellClass
(file-->new-->subclass--> UITableViewCell-->name it like CustomCellClass)
Then implement below code

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"Cell";

static BOOL nibsRegistered = NO;

if (!nibsRegistered) {

    UINib *nib = [UINib nibWithNibName:@"CustomCellClass" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
}

CustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {

    cell = [[CustomCellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.type_label.text = @"Rocky"; // setting custom cell label

return  cell;

}


来源:https://stackoverflow.com/questions/18618769/how-to-get-uitableview-label-text-string-custom-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!