Reputation: 25
I am using two UITableView
in same View
. I have used prototype cell in which there are two label
to show date and venue in TableView
.
Here is the code what I did. Both TableViews
showing same data.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableView *tableViewOne=(UITableView*)[self.view viewWithTag:5];
UITableView *tableViewtwo=(UITableView*)[self.view viewWithTag:6];
UITableViewCell *cell;
if (tableViewOne) {
NSLog(@"In Table1");
static NSString *CellIdentifier = @"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else if(tableViewtwo){
NSLog(@"In Tabl2 ");
static NSString *CellIdentifier2 = @"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
Upvotes: 0
Views: 77
Reputation: 1426
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableView *tableViewOne=(UITableView*)[self.view viewWithTag:5];
//UITableView *tableViewtwo=(UITableView*)[self.view viewWithTag:6];
UITableViewCell *cell;
if (tableView.tag==5) // problem is here
{
NSLog(@"In Table1");
static NSString *CellIdentifier = @"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else if(tableView.tag==6){
NSLog(@"In Tabl2 ");
static NSString *CellIdentifier2 = @"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
Upvotes: 0
Reputation: 17535
Your if condition is incorrect. So please check like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView.tag == 5) // Change here
{
NSLog(@"In Table1");
static NSString *CellIdentifier = @"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else
{
NSLog(@"In Tabl2 ");
static NSString *CellIdentifier2 = @"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
Upvotes: 2
Reputation: 4199
Your comaprison is incorrect since both table view are present, first condition get satisfied always thus same data in both tables.
Compare using UITableView object identifiers
if (tableView==first_table_view_name) {
}
else
{
}
Upvotes: 2
Reputation: 744
Do it like
if (tableView == tableViewOne){
// Your Code
}
elseif (tableView == tableViewTwo){
// Your Code
}
Upvotes: 2