I am implementing an iphone app. In which I have implemented the table view using custom table view cell. In each table cell there are 7 labels. I want to put the search bar in table view. Whenever I开发者_Go百科 tried to search the value for One label, the row in which this label is placed that whole doesn't come at first position but the label value get replaced by the value of first label . Mean to say that label value doesn't stick with the whole row. How to resolve this problem please provide me some solution.
Thanks very much.
This is a pretty great example of how to do a search bar:
https://github.com/erica/iOS-5-Cookbook/tree/master/C11/06-Searching%20Tables
All of the code for handling the creation and modification of the table is in the main method:
https://github.com/erica/iOS-5-Cookbook/blob/master/C11/06-Searching%20Tables/main.m
When I had this problem a while ago I ran across an article about search bars: http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view. That really helps out but it is not for custom cells.
So, use the exact same method as in the link but add this bit of code in your initWithStyle in your CustomCell.m class:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
_label1 = [[UILabel alloc] initWithFrame:CGRectMake(label1’s x position from the
upper right corner, label1’s y position from the upper right corner, label1’s width,
label1’s height)]
_label1.font = [UIFont boldSystemFontOfSize:17];
_label1.textAlignment = NSTextAlignmentLeft;
[self.contentView addSubview:_label1];
}
return self;
}
Do this to everyone of your labels and you’re good to go! :)
精彩评论