I have an application that contains 4 mutable arrays,i already displays one array of items in tableview cell,but i want to change the contents of the table cells in button click,i defined 4 buttons ,my need is when i click a button the tableview cells changes the array and display corresponding array items.I this possible to reuse tableview cell in a button click,my code for tableview cell below.
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [_titleArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Each subview in the cell will be identified by a unique tag.
static NSUInteger const kcatogeryLabelTag = 2;
static NSUInteger const ktitleLabelTag = 3;
static NSUInteger const kshortDiscriptionLabelTag = 4;
static NSUInteger const knewsImageTag = 5;
// Declare references to the subviews which will display the earthquake data.
UILabel *catogeryLabel = nil;
UILabel *titleLabel = nil;
UILabel *shortDiscriptionLabel = nil;
UIImageView *newsImage = nil;
static NSString *kEarthquakeCellID = @"EarthquakeCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kEarthquakeCellID];
if (cell == nil) {
// No reusable cell was available, so we create a new cell and configure its subviews.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kEarthquakeCellID] autorelease];
catogeryLabel = [[[UILabel alloc] initWithFrame:CGRectMake(91, 3, 190, 20)] autorelease];
catogeryLabel.tag = kcatogeryLabelTag;
catogeryLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:20];
[cell.contentView addSubview:catogeryLabel];
titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(91, 28, 170, 14)] autorelease];
titleLabel.tag = ktitleLabelTag;
titleLabel.font = [UIFont fontWithName:@"Manorama" size:10];
[cell.contentView addSubview:titleLabel];
shortDiscriptionLabel = [[[UILabel alloc] initWithFrame:CGRectMake(91, 50, 500, 20)] autorelease];
shortDiscriptionLabel.tag = kshortDiscriptionLabelTag;
shortDiscriptionLabel.font = [UIFont fontWithName:@"Manorama" size:14];
shortDiscriptionLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[cell.contentView addSubview:shortDiscriptionLabel];
newsImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loadingimage.jpg"]] autorelease];
CGRect imageFrame =newsImage.frame;
imageFrame.origin = CGPointMake(62, 2);
newsImage.frame = imageFrame;
newsImage.tag = knewsImageTag;
newsImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[cell.contentView addSubview:newsImage];
} else {
// A reusable cell was available, so we just need to get a reference to the subviews
// using their tags.
//
catogeryLabel = (UILabel *)[cell.contentView viewWithTag:kcatogeryLabelTag];
titleLabel = (UILabel *)[cell.contentView viewWithTag:ktitleLabelTag];
shortDiscriptionLabel = (UILabel *)[cell.contentView viewWithTag:kshortDiscriptionLabelTag];
newsImage = (UIImageView *)[cell.contentView viewWithTag:knewsImageTag];
}
// Get the specific earthquake for this row.
Row *newsSubcatogery = [_titleArray objectAtIndex:indexPath.row];
// Set the relevant data for each subview in the cell.
catogeryLabel.text = newsSubcatogery.categoryName;
titleLabel.text = newsSubcatogery.title;
shortDiscriptionLabel.text = newsSubcatogery.shortDescription;
NSURL *url = [NSURL URLWithString:newsSubcatogery.thumbImage];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
// CGSize imageSize = [image size];
[newsImage setImage:image];
[image release], image = nil;
return cell;
}
you have to assigned
tagproperty of table view on your button click.now in all your table view’s delegate method check the
tagof table view likeabove is sample code you have to modify it accordingly. Hope it helps you.